Internal classes are generally referred to as nested classes.
As the name suggests, a class or interface has a class inside it.
The main purpose is to group classes together. Why do we need this?
class Outer_Class{
//OuterClass members
class Inner_Class{
//InnerClass members
}
}
The internal categories are divided into four main types, each with different functions in mind:
The following is a comparison of the differences between the general and static inner class.
class Outer_Class {
int num = 12;
class Inner_Class {
int num2 = 6;
}
}
Outer_Class Outer = new Outer_Class();
/*Outer_name.Inner_name Inner_objname = Outer_objname.new Inner_name()*/
Outer_Class.Inner_Class Inner = Outer.new Inner_Class();
public class Main {
public static void main(String[] args) {
Outer_Class Outer = new Outer_Class();
Outer_Class.Inner_Class Inner = Outer.new Inner_Class();
System.out.println(Outer.num * Inner.num2);
}
}
class Outer_Class {
int num = 12;
class Inner_Class {
public int Inner_Function(){
return num;
}
}
}
public class Main {
public static void main(String[] args) {
Outer_Class Outer = new Outer_Class();
Outer_Class.Inner_Class Inner = Outer.new Inner_Class();
System.out.println(Inner.Inner_Function());
}
}
Internal classes can access variable functions of external classes; object instantiation of internal classes via external classes
Static internal classes As with the static functions mentioned earlier, here you do not need to create objects via an external class to access internal members.
class Outer_Class {
int num = 12;
static class Inner_Class {
int num2 = 6;
}
}
public class Main {
public static void main(String[] args) {
Outer_Class.Inner_Class Inner = new Outer_Class.Inner_Class();
System.out.println(Inner.num2);
}
}