java foundation note 009 - internal class + keyword
Today's content
1. [Understand] what is an internal class 2. [Master] anonymous inner class 4. [[understanding] final Use of keywords 5. [Understand the definition and use of package 6. [[understand] permission modifier 7. [Master] static Use of keywords
1, Inner class
1.1 concept
The inner class A is defined in a class B. A is the inner class of B and B is the outer class of A
public class B{ public class A{ //Member inner class } public void method(){ class D{ //Local inner class } } } class C{ //C is not an inner class of B, but a sibling class }
1.2 internal classification
1. Member inner class 2, local inner class 3, anonymous inner class
1.2.1 member internal class
- Definition and format
Defined in the class of a class, the class outside the method is the class inside the member.
class B{ class A{ } }
- Member internal class characteristics
1.Internal members can directly access external class members, including private members. 2.An external class must create an internal class object to access members of an internal class. 3.Member internal classes can be created and used in external classes and other classes.
-
Object creation of inner class of member
-
In its external class, it is used normally
Member internal class name variable name = new Member internal class name();
-
In other classes
External class.Internal class variable name = new External class().new Inner class();
-
-
Member internal class compilation generated. Class file format
The independent class file will also be generated, but the external class name will be added in front of it in a fixed format: external class name $internal class name. Class
1.2.2 local internal class
- Definition and format
A class in a common method defined in a class is a local inner class.
public class B{ public void method(){ int i = 9; class A{ //A is the local inner class of B } } }
-
Characteristics of local inner classes
1.Local inner classes, like local variables, are defined in methods in classes 2.Local internal classes are created with the creation of methods, and will be recycled after the method is executed 3.A local inner class can only create and use objects in the method that defines it 4.All members of the external class, including private ones, can also be manipulated in the local inner class
-
Object creation of local internal classes
A local inner class can only create and use objects in the method that defines itpublic class B{ public void method(){ int i = 9; class A{ //A is the local inner class of B public void show(){ System.out.println("Local internal class method execution"); } } //Create and use local inner class objects in methods A a = new A(); a.show(); } }
1.2.3 anonymous inner class
-
Definition and format
An anonymous inner class is an anonymous subclass object that implements / overrides the parent interface or parent class.//format Parent class/Parent interface variable name = new Parent class/Parent interface(){ //override method ...... };
Take the interface as an example:
//Interface public interface B{ void show(); } //main method of test class public class Test{ public static void main(String[] args){ B b = new B(){ //Method of implementing interface B public void show(){ System.out.println("Anonymous inner class method execution"); } }; } }
-
Characteristics of anonymous inner classes
1.An anonymous inner class must be an assignment to a variable declared by a parent class or parent interface. 2.When the parent class declares that the variable is assigned with an anonymous inner class, the variable can only operate on the parent class properties and methods overridden by the anonymous inner class. Of course, the original non abstract methods in the parent class can be called through the variable even if they are not overridden. Subclass specific methods cannot be called. 3.When the parent interface declares that the variable is assigned with an anonymous inner class, the variable can only operate the interface methods implemented by the anonymous inner class. Of course, the original static methods and default methods of the interface can be called through the variable even if they are not rewritten. Subclass specific methods cannot be called. 4.The essence is polymorphism.
2, final keyword
2.1 interpretation
Final keyword, with a proper interpretation: final. Can be used to modify classes, methods, and variables.
2.2 final use
2.2.1 decoration
-
Modified class cannot be inherited. And cannot modify abstract classes and interfaces.
-
format
final class{ }
2.2.2 modification method
-
Modified methods can be inherited and cannot be overridden. And cannot modify abstract methods.
-
format
Access modifier final Return value type method name(){ Method body }
2.2.3 modification variables
-
Modified variables are called custom constants,
-
1. Modified local variable - basic data type. It can only be assigned once. If it is assigned again, an error will be reported. It can be assigned when it is declared or after it is declared.
public class A{ public void show(){ final int a = 9; final int b; b=3; //a=1; Assign the value again and report an error, the same below //b=4; } }
-
2. Decorated member variable - basic data type. It can only be assigned once, and then an error is reported. There are three assignment methods
//1. Assignment method 1, assignment during declaration, the most commonly used public class A{ final int a = 9; } //2. Assignment in construction method. Here, all custom construction methods need to assign values to the custom constant. public class A{ final int a; public A(){ a=9; } public A(int a){ this.a = a; } } //3. Static code block assignment, but static shall be added before final keyword public class A{ static final int a; static { a = 5; } }
-
The reference data type can only be assigned once, but the reference data type storage involves stack, heap and method area (including constant pool), so the address in the heap where the reference data variable (in the stack) is stored cannot be changed, but the content of the constant pool stored in the heap can be changed.
public class A{ public void show(){ final B b = new B(); //b = new B(); report errors b.Attribute 1 = 4; //No error reporting b.Attribute 1 = 6; //No error reporting } }
-
3, package
Definition and use of package
Package: essentially a folder
-
Create package: (single level package, multi-level package)
-
Use "." to split multi-level packages
-
Definition and specification of multi-level package: website address reversal of the company (remove www)
-
-
Naming rules for packages: all letters are lowercase
Www.baidu.com -- > name of module function
com.baidu. Name of current project. Name of module function
4, Access modifier
4.1 four access modifiers
The access permissions are changed from large ---------------------------
Public public protected default private
4.2 access control scope
When an access modifier modifies a property or method | Current class | Same as package | Children and grandchildren | Other package s |
---|---|---|---|---|
Public / public | √ | √ | √ | √ |
Protected / protected | √ | √ | √ | × (accessible to descendants) |
Do not write, default access modifier | √ | √ | √ (not accessible for different packages) | × |
Private / private | √ | × | × | × |
5, static keyword
5.1 static interpretation
Static static, modifiable variables, methods, and static code blocks represent classes. Static modifiable content is created only in use. All such objects are shared in the method area.
5.2 use of static
5.2.1 modification variables
-
Modified variables, called static variables, can be used directly by (class name. Variable name) and shared by all such objects.
public class A{ static int a = 9; } //Test class public class Test{ public static void main(String[] args){ A a1 = new A(); A a2 = new A(); //The following three calls are correct, and the operations on static variable a are superimposed on each other. a1.a = 8; a2.a = 4; A.a = 1; } }
5.2.2 modification method
-
Modification methods, called static methods, can be used directly by (class name. Method). All objects of this class share and cannot be overridden by subclasses.
public class A{ public static void show(){ System.out.println("Static method"); } } //Test class public class Test{ public static void main(String[] args){ A a1 = new A(); A a2 = new A(); //The following three calls are correct. a1.show(); a2.show(); A.show(); } }
5.2.3 modifier code block
-
A decorated code block (that is, a code collection in curly braces {}) is called a static code block. The class is loaded only once when it is first used.
public class A{ //Static code block static { Code block; } }
5.2.4 precautions for static use
- Static methods can only call static methods and static member variables, not ordinary methods and ordinary member variables.
- The static modification is executed once when the class is loaded, and all objects of this class share.
- static modifies the content storage structure. Refer to: Understanding static and final keywords from the perspective of memory - brief book
5.3 class loading composition with static modifier
reference resources: Loading order of classes when creating objects in java_ samuBO blog - CSDN blog
|Parent static variable or parent static code block [see position, execute sequentially, from top to bottom]
|Subclass static variable or subclass static code block [look at the position, execute sequentially, from top to bottom]
|Parent class non static variable or parent class non static code block [look at the position, execute sequentially, from top to bottom]
|Parent class construction method
|Subclass non static variable or subclass non static code block [look at the position, execute sequentially, from top to bottom]
|Subclass construction method