There are four types of internal classes in Java:
1. Static inner class: As a static member of a class, exists inside a class.
a static inner class is a member of an external class, but an object of a static inner class can be created directly without creating an object of the outer class. Static inner classes can refer to static member variables and static methods of external classes, but cannot refer to ordinary members of external classes.
Test procedures for static internal classes
public class Outter {
static int a=1;
int b=5;
static void Test () {
System.out.println ("Static Methods for external classes");
}
Static Class inner{
public void Test2 () {
System.out.println (the value of "a" is "+a");; /directly referencing static member variables of external classes
Test ();//static method that directly references an external class
b++, attempting to reference a non-static member variable of an external class, cannot be compiled by the
System.out.println ("Methods of static internal classes");
}
}
public static void Main (string[] args) {
Inner in=new Inner ()///The object of a static inner class can be created directly without first creating an object of the outer class
In.test2 ();
}
}
2. member Inner class: As a member of a class, exists inside a class.
A member inner class can invoke all members of an external class, but only after an object of the outer class has been created can the external member be called.
public class Outter1 {
static int a=1;
int b=0;
public static void Test1 () {
System.out.println ("Static Methods for external classes");
}
public void Test2 () {
SYSTEM.OUT.PRINTLN ("Non-static method of the external class");
}
Class inner{
public void Test () {
System.out.println ("within the method of the member's Inner class");
Test1 ();//Call the static method of the external class
Test2 ();//calling Non-static methods of external classes
System.out.println (a+b);//accessing Static and non-static member variables of external classes
}
}
public static void Main (string[] args) {
Inner in=new Inner (); objects within a member's inner class cannot be created directly and will be error-
Outter1 out=new Outter1 ();//Create an object of the outer class first
Inner in=out.new Inner ();//NOTE:!! An object of a member's inner class must be created from an object of an external class
}
}
3. Local inner class: exists in the interior of a method.
The local inner class can only be used inside the method, and once the method executes, the local inner class is removed from memory.
It is important to note that if a local variable in his method is to be used in a local inner class, then the local variable needs to be defined as final.
public class Outter2 {
int a=10;
public void Test () {
final int c=5;
System.out.println ("In the method of the outer class");
Class inner{
int b=20;
void Test1 () {
System.out.println ("In the method of the local inner class");
System.out.println (c);///NOTE: If you want to use a local variable in his method in a local inner class, you need to define the local variable as final.
}
}
Inner inner=new Inner ();
Inner.test1 ();
}
public static void Main (string[] args) {
Outter2 outter=new Outter2 ();
Outter.test ();
}
}
4. Anonymous inner class: A class that exists inside a class but has no class name.
The definition of an anonymous inner class is combined with the creation of an object, the anonymous inner class is typically defined by the following form, and the object is instantiated at the same time as defined.
The name of the new class or interface () {
The body of the anonymous inner class, which is the body of the anonymous inner class, is the implementation of the class or interface, and if it is a class , the anonymous inner class is the subclass of the class, and if it is an interface, the anonymous inner class needs to complete the implementation of the interface.
}
Class person{
public void Show (Message message) {
Message.show ();
}
}
Class message{
public void Show () {
System.out.println ("In the Message Class");
}
}
public class Outter3 {
public static void Main (string[] args) {
Person Person=new person ();
Person.show (New Message () {
public void Show () {
System.out.println ("in Anonymous inner class");
}
});
}
}
}
Basic knowledge of four internal classes in Java