On the four inner classes in Java

Outline

If you've seen some of the JDK and framework source code, then it is often found in the general definition of class, will then define some of the other classes will also be compiled into byte code file, this class was named interior class , according to the general division method can be divided into the following four categories:

  • Members of the inner class
  • Partial inner class
  • Anonymous inner classes
  • Static inner classes

Next will be explained in detail against the four inner classes, these classes are intended to explain the features and application scenarios, if you bother to look, you can turn directly to the bottom to see the summary

We have a pre-defined class, then come in all operations will be executed in this class:

class Demo {
    
    public int pubVal = 1;
    
    private int priVal = 1;
    
    public static void staticMethod() {}
    
    public void publicMethod() {}
    
    private void privateMethod() {}
    
}
复制代码

Members of the inner class

concept

Can be seen from the name, such as the interior member of the class of the class exists, located inside the class definition, it can be appreciated as a member variable analog

achieve
class Demo {

	// ...	
	
    class InternalClass {
        
    }
    
    // ...
    
}
复制代码

So is a member of the inner class

constraint

The first is the class definition constraints, in fact, members of the class definition inside the class without any constraint (does not involve static, because it belongs to the category of static inner classes), not only inside the class can be declared as public, private, and can even be declared as an abstract class and interface abstract interfaces, and external class inheritance is allowed, which is defined as very loose

Followed by internal properties and methods, where there are a number of constraints:

  • Properties and methods can not be modified any member of the class uses an internal static, but can use any modifier remaining private, final etc.
  • It can be used to modify the static final member
  • Class with the same name may allow external fields and methods

In other words, members of the inner class can not exist static properties and methods, it also acted within the meaning of the member variables

Finally, constraints on access, here is important:

  • Members of inner classes there this pointer, but the pointer is a reference to itself, if you want to access outside the class, you need to use an external class name .this pointer
  • By external name class. Static field to access a static class property or method of the external field

Restrictions on where and members of the methods are similar analogy can be made in understanding

use

First is a statement to the program this article as an example, we can declare an inner class in the following way:

        Demo.InternalClass demo
复制代码

Note, if you will this inner class declared private, and still have access to the outer class, but other classes is not accessible to the outside of the outer class

Then is created by new, we are outside of any class of non-static method , the can be created using the new, specific format is as follows:

        Demo.InternalClass demo = new Demo.InternalClass();
复制代码

Similarly, we may be the outer class, and a method to access private variables inside inner class by way of example

Partial inner class

We just spent a lot of effort to explain the inner class members, and members of the local inner classes and inner classes are very similar, probably some of the same places I have alluded to the

concept

Partial inner class class members located outside the internal process, local variables can be analog

achieve
    public void publicMethod() {
        class InternalClass {
            
        }
    }
复制代码
constraint

Here's the basic constraints and similar members of inner classes, I would say that the individual number of different places:

  • Modifiers are not allowed on any of its class, but you can use abstract classes declared as an abstract class
  • Do not allow the local inner class declared interfaces
  • We are not allowed to declare static member variables and methods
  • It may be partially inside the class declaration in a static method
  • The method of any two local internal class can be the same name

The remaining members of the inner class basic and similar to the local inner class version as local variables within the class members like to understand, for example, also has a pointer to the outer class, using the same methods and members of the inner class

use

Since it is a partial internal class, the method can only be used in the class declaration, the declaration and use as follows:

    public void publicMethod() {
        class InternalClass {

        }

        InternalClass test = new InternalClass();
    }
复制代码

Similarly, we can still access the internal unconditional private property defined in the class

Anonymous inner classes

This is the largest inside a class should we use, sometimes we have already used it but did not find

concept

Disclaimer anonymous inner class has no class, implicitly inherit a class or implement an interface

achieve

More abstract concept, us directly to see how to define, here we have a target accepts a parameter of the method:

    private void privateMethod() {
        new Demo() {
            public int newVal = 20;

            @Override
            public void publicMethod() {
                super.publicMethod();
            }
        };
    }
复制代码

Demo here can be replaced with any class or interface, you will find this class has no name, so called anonymous inner classes

If Demo is a general category, the anonymous inner classes corresponding to a subclass of this class; if Demo or abstract class is an interface, the anonymous inner classes corresponding to implement the abstract class interface or

constraint

Methods want to understand the constraints anonymous inner class, you need to do the whole anonymous inner class as a program logic point of view, but should be treated as an object, block anonymous inner classes can be used as a complete object, you can call the object's attributes and many more

The main constraint has the following:

  • You can not use static methods and properties to modify, but can have properties of static final
  • Variables can be inherited and use this pointer to access the definition itself, may be used externally .this class name pointer to access all of the properties of the outer class
  • We can not make any modification in the class, because there is no definition of class and class name
  • Which defines private fields outside is fully visible
use

Here is not too much to explain the conduct, complete and consistent use of ordinary objects, here we give two simple examples:

        int val = new Demo() {
            private int newVal = 20;

            @Override
            public void publicMethod() {
                super.publicMethod();
            }
        }.newVal;
        
        Demo demo = new Demo() {
            private int newVal = 20;

            @Override
            public void publicMethod() {
                super.publicMethod();
            }
        };
复制代码

Static inner classes

concept

Static inner classes equivalent static modification of members of the inner class, can be understood as a static variable

achieve
class Demo {

	// ...	
	
    static class InternalClass {
        
    }
    
    // ...
    
}
复制代码
constraint

Constraints on here before and are very different, as follows:

  • Can be modified classes, methods and properties of any modifier
  • Static methods and properties can be accessed outside the class

Yes, broadly, there is no constraint static inner classes

use

Use the following two situations:

As used in the method outside the class, the class object is created inside by the following statement:

		InternalClass test = new InternalClass();
复制代码

In addition to the external of other classes can be created inside the class object with the following statement:

        Demo.InternalClass test = new Demo.InternalClass();
复制代码

to sum up

Finally, we summarize a table, basically knowledge within the class are here:

Members of the inner class Partial inner class Anonymous inner classes Static inner classes
analogy Member variables Local variables Implementation class or subclass interface Static variables
Class declaration of position Inner class Internal Scope The position of any object that may arise Consistent with the members of the inner class
Class modifier In addition to static outside any Only allowed with abstract modification Can not be modified Any modifiers can
Variables and methods modifiers Except insofar as any static (but can be modified with a static final variable) Consistent with the members of the inner class Consistent with the members of the inner class Can any modifiers
Access outside the class Can Can Can Can
Location object declaration Only members of the outer class method, and the rest declared in the class and outside of class outside (if outside the outer class, depending on the type of access control is allowed) Only declaratory domain No need to declare Can be any position (if outside the outer class, class depends on whether to allow access control)
Objects created position In the method can only be created outside of class members Can only be created in scope Create Now Can be any position (if outside the outer class, class depends on whether to allow access control)
Whether to allow internal class inner class Yes, but it does not allow static inner classes Consistent with the members of the inner class Consistent with the members of the inner class can

Guess you like

Origin juejin.im/post/5cef627c51882505202d0ae9