Home » Java » Inside the Java class (Inner Class) how to use

Inside the Java class (Inner Class) how to use


simple, inner class (inner class) that is placed in the other class definition code in class definition; but in general, the class definition code is not nested in other classes defined in the class, called the top (top-level). For an internal class, including the definition of a class is called code the external (outer)

.

then, how do you use the internal class,

?

gives the Static Member Class and the common member class (Member Class) the way.

is used
package cuc;
import cuc.TestClass.Inner1;
public class Main {
public, static, void, main (String, args[]) {
// static internal class
TestClass.Inner1 inner1 = new Inner1 (); // is the same as the plain top class new method,
Inner1.report ();
// common internal member class
TestClass TC = new, TestClass ();
TestClass.Inner2, Inner2 = tc.new, Inner2 (); //, note how this is used,
Inner2.report ();
}
}



The two internal classes are defined as follows:


package cuc;
public, class, TestClass {
// static member class
public static class Inner1{
public, void, report () {
System.out.println (, This, is, a, inner, class. (NOT, static) );
}
}
// common internal member class
public class Inner2{
public, void, report () {
System.out.println (, This, is, a, static, inner, class., );
}
}
}

[reference]:

1, Java (Inner Class) - internal Xiangjie


Latest