Inner class

Inner class: a class defined within a class. Although this feature is known in Java, it is rarely used in practice, except when looking at the source code.

5.1 Why inner classes are Used:

(1) Because Java can only be single-inherited, multiple inheritance can be implemented using inner classes, so each inner class can inherit independently from an implementation. It does not matter whether the enclosing class already inherits an implementation. ② The inner class can access all members of the outer class object. ③ If the class needs to be instantiated only once, you can use anonymous inner classes.

5.2 Why can an inner class access all members of an outer class object

Because. Eg: Declare a class with an inner class: because the compiler modifies all the constructors of the inner class, adding an argument referenced by the outer class


    public class OuterClass{
        class InnerClass{}
    }
Copy the code

Javac is compiled with two files, because each class produces a.class file, and the inner classes are named according to strict rules, the outer class dollar sign plus the inner class, as follows


class OuterClass$InnerClass {
    OuterClass$InnerClass(OuterClass var1) {
        this.this$0 = var1;
    }
    private void study() {
    }
}
Copy the code

3. The method and the scope of the inner class (1) a class definition in method (2) a definition in the scope of the class, the scope in the interior of the method (3) a anonymous class implements an interface (4) an anonymous class, he expanded the class has a default constructor (5) an anonymous class, it executes field initialized 6. An anonymous class, An inner class accesses local variables, which must be constant. Static nested classes are different from inner classes: A statically nested class can be completely independent of the enclosing class to hide itself, while the inner class is a part of the enclosing class. The inner class object is provided that the outer class object exists.

Six, generics

6.1 Why Generics?

Because the collection can be added with elements of type Object, which means that any subclass of Object can enter the collection with no problems at compiler or runtime. When we get the collection element, we get an Object reference, but we can’t force multiple specific types through traversals. Unless there are more than one branch instance of judging the type. So we decided to use generics, enclosing the type parameters in Angle brackets, which specify the types that the collection can hold and prevent objects of the wrong type from being put into the collection at compile time.

6.2 Generic definitions

1) define the class


6.3 Generic erasure

Virtual machine without the generic type Object. All objects belong to common class, whenever you define a generic type, automatically provides a corresponding primitive types, the name of the original type is delete after the type parameter of the generic type name, erasing type variable to erase its first boundary, and replace with qualified types (type of unrestricted use Object), <> cannot use primitive datatype, use reference type because generic erasure. Only Object (reference type). T → Object T extends Comparable& Serializable → Comparable 4. If generic erasure, why return a specific type? The compiler will automatically force the returned Object type to a specific type. Also because of generic erasure, the following problems occur:



    public void study(Object obj){
        study((String)obj);
    }
Copy the code

When we define a method whose return type is generic and the subclass overrides its parent, the subclass has two methods: a generic-erased Object return type method and a type-specific method. Both methods have the same method signature. We know that a class cannot have two methods with the same signature. This is illegal, but in virtual machines return types can be used to distinguish methods. If you want to override a generic method, you can override it. If you want to override a generic method, you can override it. If you want to override a generic method, you can override it

6.4 the wildcard

T Custom generic types? The default is Object and its subclasses, all objects in Java, right? Extends T accepts an object of type T or a subtype of T? Super T accepts objects of type T or a supertype of T

Seven, abnormal

7.1 Abnormal System:


7.2 Throwing an Exception

Throw exception generation stage: manually throw an exception object throws all kinds of exception classes that the declaration method may throw. The exceptions thrown can be caught by catch or continue to be thrown to the upper layer. Pay attention to the rewriting restriction. And the most detailed catch exception is written first and more general after. In the project, there are generally custom exceptions and different error codes for different error scenarios. In the whole project, the error codes defined by systems in charge of different departments will be different. In this way, we can quickly find out which link is wrong according to the error codes directly during troubleshooting. Note:

7.3. The finally

Since the code in finally always executes, it is generally used to close resource objects (in JDK7 and above, for objects that implement the java.lang.AutoCloseable interface, And objects that implement the java.io.Closeable interface can use try — with — resources). It is best not to use a return in finally.