catalogue
2, Classification of internal classes
1, Concept of inner class
In Java, a class can be defined inside another class or a method. The former is called an internal class and the latter is called an external class. Inner classes are also an embodiment of encapsulation.
public class OutClass {//External class class InnerClass{//Inner class } }
matters needing attention:
1. The inner class must be defined in the class name {}. The inner class defined outside the class name {}, even in a file, cannot be called an inner class. For example:
public class A{ } class B{ } //A and B are independent, and there is no theory of internal and external classes
2. The internal class and external class share the same java source file, but after compilation, the internal class will form a separate bytecode file.
2, Classification of internal classes
public class OutClass { //General inner class class InnerClass1{ } //Static inner class static class InnerClass2{ } { //A local inner class defined in an instance code block class InnerClass3{ } } static{ //Local inner classes defined in static code blocks class InnerClass4{ } } public void method(){ //Local inner classes defined in methods class InnerClass5{ } { //Local inner classes can also be defined in ordinary code blocks in methods class InnerClass6{ } } } }
Depending on the location of the internal class definition, it can be divided into:
Member inner classes: normal inner classes and static inner classes.
Local inner classes and anonymous inner classes.
3, Member inner class
1. General inner class
Member inner class not modified by static.
public class OutClass {//External class private int a; private int b; private int c; public void method1(){ a=100; System.out.println(a); } public void method2(){ System.out.println(b); } class InnerClass{ //General inner class private int c; //An internal member variable with the same name as the external class member variable public void methodInner() { //In the inner class, you can directly access the member variables and methods of any qualifier of the outer class b = 150; method1(); method2(); //When accessing a member with the same name as the external class member, the internal class's own member is accessed first c = 300; System.out.println(c); //To access a member with the same name in an external class, you must use -- external class name. this. Member name OutClass.this.c = 400; System.out.println(OutClass.this.c); } } public static void main(String[] args) { //Create an internal class object OutClass.InnerClass innerclass1=new OutClass().new InnerClass(); //You can also create an external class object before creating an internal class object OutClass outclass=new OutClass(); //Note that when creating internal class objects in different files, the external class name of the internal class must be added. It can be added or not in the same file OutClass.InnerClass innerclass2=outclass.new InnerClass(); innerclass2.methodInner(); } } /* 100 150 300 400 */
matters needing attention:
1. All methods in the external class can be accessed directly in the ordinary internal class.
2. The location of ordinary internal class members is the same as that of external class members, which are restricted by public,private and other qualifiers.
3. When accessing a member with the same name in an ordinary internal class method, you have priority to access your own. If you want to access a member with the same name in an external class, you must: access a member with the same name in the external class name. this.
4. To create a normal inner class object, you must first create an object of an outer class. When creating an internal class object, if the internal class is in the current file, the external class name is not required at the beginning of the object creation statement. If it is in a different file, the external class name of the internal class must be indicated.
5, A normal inner class contains a reference to an outer class object.
6. In the external class, you cannot directly access the members in the internal class. If you want to access, you must first create the object of the internal class.
public class OutClass1 { private int a; public void method(){ System.out.println(c); //Directly access the internal class members in the external class, and compile and report an error } class InnerClass{ private int c; } }
public class OutClass1 { private int a; public void methodOut(){ InnerClass I2=new InnerClass();//Creating an internal class object in an external class method can be created directly System.out.println(I2.c); } class InnerClass{ private int c; } }
From a heap and stack perspective, internal and external classes:
2. Static inner class
A member inner class modified by static is called a static inner class.
public class OutClass {//External class private int a; static int b; //Static variable public void method1(){ } public static void method2(){ //Static method } static class InnerClass{ //Static inner class public void methodInner() { //Only static members and methods of external classes can be accessed in static inner classes // a=100; Compilation failed, a is not a member variable of an external class b = 150; // method1(); Compilation failed, method1 is not a member method of an external class method2(); } } public static void main(String[] args) { //Creation of static inner class InnerClass innerclass=new InnerClass(); //Member access innerclass.methodInner(); } }
matters needing attention:
1. In an inner class, only static members in an outer class can be accessed.
2. When you create a static internal class object, you do not need to create an external class object first.
3. The internal class of the member will generate its own independent bytecode file after compilation, for example:
4, Local inner class
If it is defined in the method body or {} of an external class, the internal class can only be used where it is defined.
public class OutClass {//External class int a=10; public void method(){ //External class method int b=10; class InnerClass{ //Local inner class, which can only be used inside the method body public void methodInnerClass(){ System.out.println(a); System.out.println(b); } } InnerClass innerclass=new InnerClass(); innerclass.methodInnerClass(); } public static void main(String[] args) { //InnerClass innerclass=null; Compilation failed } }
matters needing attention:
1. A local inner class can only be used inside a defined method body.
2. Local internal classes cannot be modified by public, static and other qualifiers.
3. Similarly, after compilation, the local internal class will generate its own bytecode file.
Naming format: external class name $X internal class name. Class, where x is an integer.
5, Object printing
public class Person { String name; String gender; int age; public Person(String name,String gender,int age){ this.name=name; this.gender=gender; this.age=age; } public static void main(String[] args) { Person person=new Person("luka","man",21); System.out.println(person); } }
Print results: Not the object itself.
If you want to print the object itself, you need to override the toString method.
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
public String toString() { //toString method overridden return "[" + name + "," + gender + "," + age + "]"; }
public class Person { String name; String gender; int age; public Person(String name,String gender,int age){ this.name=name; this.gender=gender; this.age=age; } public String toString() { //toString method overridden return "[" + name + "," + gender + "," + age + "]"; } public static void main(String[] args) { Person person=new Person("luka","man",21); System.out.println(person); } } /* Execution results: [luka,man,21] */