I still remember that when I just learned the internal class, I was often confused about the external class and all kinds of internal classes. When I knew how to do it later, with the passage of time, I had to say something about it but I could not recall anything. Therefore, this paper made a review of the internal class.
The definition of an inner class is that you can put the definition of one class inside the definition of another. Inner classes are a very useful feature that allows you to group logically related classes together and control the visibility of the classes that reside inside them. For the various inner classes mentioned above, we can roughly divide them into the following categories:
The inner class
As can be seen from the figure above, inner classes can be divided into static inner classes and non-static inner classes, while non-static inner classes can be divided into local inner classes and anonymous inner classes. Meanwhile, we call the classes that wrap inner classes as outer classes, as follows:
class OuterClass {...// Static inner class
static class StaticInnerClass {... }// Non-static inner class
class InnerClass {... }}Copy the code
External classes can be decorated with public or default package permissions, while inner classes can be decorated with private, protected, public, and package permissions. Use inner classes and we usually use ordinary class and no different, in addition when generating the inner class object, the inner class holds the current external references, through this reference it can access all external class member variables, including private variables, that is actually inside class and outside class instance it is in the object is associated with it, It can’t exist without an external class instance, so we might wonder, how does it relate to an external class? The compiler generates Java bytecode by adding constructors to non-static inner classes that reference the outer class when instantiated.
Since an inner class needs to hold a reference to an outer class object when it is built, we can create the inner class object using the object of the outer class, as follows:
public class OuterClass {
public class Inner {}
public static void main(String[] args) {
OuterClass out = new OuterClass();
OuterClass.Inner in = out.new Inner();
// It can also be created this wayInner oin = out.new Inner(); }}Copy the code
Static inner class
When we don’t want to there is a link between internal class objects and their external class object, the inner class can be declared as static, so it became a static inner class, since there is no any associated class object and external, so it does not need to rely on and outside class objects, so that it can’t access external non-static member of the class, Only static members of an external class can be accessed, and static inner classes and non-static inner classes are created slightly differently:
OuterClass is an external class. The OuterClass contains a static inner class S and a non-static inner class I
OuterClass out = new OuterClass();
// Create a static inner class
out.S staticClass = out.S();
// Create a non-static inner class
out.I in = out.new I();
Copy the code
Here’s a summary of static and non-static inner classes:
- Static inner classes can have static members, while non-static inner classes cannot have static members.
- A static inner class can access static variables of an external class, but not non-static variables of an external class.
- Non-static members of a non-static inner class can access non-static variables of the outer class.
- The creation of a static inner class does not depend on the creation of an external class, whereas a non-static inner class must depend on the creation of an external class.
Local inner class
If an inner class is only used in a method, then we can define the inner class inside the method. This inner class is called a local inner class, and its scope is limited to the method area, as follows:
public class OuterClass {
public void show(a) {
System.out.println("External class method");
}
public void showFunctionClass(a) {
class FunctionClass {
private void show(a) {
System.out.println("I'm a local inner class.");
}
}
FunctionClass FunctionClass = new FunctionClass();
FunctionClass.show();
}
public static void main(String[] args) {
OuterClass out = newOuterClass(); out.show(); out.showFunctionClass(); }}// Result:
// External class methods
// I am a local inner class
Copy the code
In addition, local inner class notes are as follows:
- A local inner class is hidden from the outside world and can only be accessed through the method that created the class.
- Local inner classes do not allow access modifiers;
Anonymous inner class
An anonymous inner class is a local class without a class name. An anonymous inner class allows the class to be defined and instantiated at the same time, so it is often used to simplify code writing. Since it has no class name, there is no constructor.
public interface Person {
public void eat(a);
}
public class Child implements Person {
@Override
public void eat(a) {
// TODO Auto-generated method stub
System.out.println("eat...");
}
public static void main(String[] args) {
Child child = newChild(); child.eat(); }}Copy the code
This is the normal way to write an anonymous inner class without using an anonymous inner class. Implement the interface, override the methods, and instantiate the object and call the methods.
public class Child {
public static void main(String[] args) {
new Person() {
@Override
public void eat(a) {
// TODO Auto-generated method stub
System.out.println("eat..."); } }.eat(); }}Copy the code
So, as long as a class is an abstract class or an interface, its subclass methods or interface methods can be implemented using an anonymous inner class. A typical scenario for anonymous inner classes is to implement multithreading using the Thread class or the Runnable interface. It is also important to note that when an anonymous inner class uses an object defined outside of it, the compiler requires that the variable be final for the sake of consistency.
Why do WE need inner classes
In Thinking in Java, the author says that inner classes are most interesting because:
Each inner class can inherit independently from an implementation, so it doesn’t matter whether the enclosing class already inherits an implementation.
See here, I want to say that the master is the master, said I have a face meng forced (too vegetables), summed up the next or have the following points:
- Inner class methods can access data in the scope in which the class is defined, including private data decorated by private;
- Inner classes can be hidden from other classes in the same package;
- Inner classes can address the pitfalls of Java single inheritance;
- For some callback functions with a lot of redundant code, anonymous inner classes can be implemented.
Reference: Thinking in Java
Push the information
- We are recruiting partners, interested partners can send your resume to [email protected], note: from the nuggets community
- For details, please click here –> Guangzhou Reed Information Technology