An inner class is a class defined inside another class. Inner classes can, in fact, be constructed in several contexts. An inner class defined as a member of a class can be instantiated anywhere in that class. An inner class defined inside a method can only be referred to later in the same method. Inner classes can also be named or anonymous.
package org.kodejava.basic;
public class InnerClassDemo {
private Bean bean;
/**
* Inner class, the compiled class will be named InnerClassDemo$Bean.class
*/
class Bean {
public int width;
public int height;
@Override
public String toString() {
return width + " x " + height;
}
}
public InnerClassDemo() {
Bean bean = new Bean();
bean.width = 100;
bean.height = 200;
this.bean = bean;
}
public Bean getBean() {
return this.bean;
}
public static void main(String[] args) {
InnerClassDemo inner = new InnerClassDemo();
System.out.println("inner.getBean() = " + inner.getBean());
}
}
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024