1 final keyword
1.1 Definition
The final keyword represents final and unchangeable.
1.2 Usage
Four common uses:
1. Can be used to modify a class
public final class class name {
//…
}
Meaning: The current class cannot have any subclasses, but it can have a parent class. (eunuch type)
Note: If a class is final, all member methods in it cannot be overridden or overridden. (Because I have no son)
public final class MyClass { public void method(){ System.out.println("Method execution!"); } }
2. Can be used to modify a method
Modifier final return value type method name (parameter list) {
//Method body
}
Meaning: When the final keyword is used to modify a method, this method is the final method, that is, it cannot be overridden or rewritten.
Note: For classes and methods, the abstract keyword and the final keyword cannot be used at the same time because of contradiction.
public class Fu { public final void method(){ System.out.println("Parent class method execution"); } }
3. Can be used to modify a local variable
Precautions:
- For basic types, immutability means that the data in the variable cannot be changed.
- For reference types, immutability means that the address in the variable cannot be changed.
public class Student { private String name; public Student() { } public Student(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } }
public class Final { public static void main(String[] args) { int num=20; System.out.println(num); num=10; System.out.println(num); //Once final is used to modify a local variable, the variable cannot be changed. //"Assign a value once and it will remain unchanged for life" final int num2=50; System.out.println(num2); //num2=250;//Wrong writing method, cannot be changed! //Correct writing method, as long as there is only one assignment final int num3; num3=90; //For basic types, immutability means that the data in the variables cannot be changed. //For reference types, immutability means that the address value in the variable is immutable. Student stu1=new Student("王五"); System.out.println(stu1); System.out.println(stu1.getName()); stu1=new Student("赵四"); System.out.println(stu1); System.out.println(stu1.getName()); final Student stu2=new Student("cici"); System.out.println(stu2.getName()); //Wrong writing method, final reference type variable, the address cannot be changed] // stu2=new Student("sdff"); } }
4. Can be used to modify a member variable
For member variables, if modified with the final keyword, the variable will remain immutable.
1. Since member variables have default values, they must be assigned manually after using final, and default values will no longer be given.
2. For final member variables, either use direct assignment or assignment through the constructor method. Choose one of the two
3. It must be ensured that all overloaded constructors in the class will eventually assign values to final member variables.
public class Person { //Direct assignment, delete assignment statements in all methods // private final String name="sfdsg"; //Indirect assignment, add assignment to the parameterless construction, and delete the setter method private final String name; public Person(String name) { this.name = name; } public Person() { name="cici"; } // public void setName(String name) { // this.name = name; // } public String getName() { return name; } }
Four permission modifiers in Java:
public > protected > (default) > private
Same class (myself) yes yes yes yes
Same package (my neighbor) yes yes yes yes
Different types of buns (my son) yes yes no no
Different package non-subclass (stranger) yes no no no
Note: (default) is not a keyword, there is no need to write it at all
2 Internal class
2.1 Definition
If one thing contains another thing inside, then a class contains another class inside
For example: the relationship between the body and the heart, the relationship between the car and the engine
2.2 Classification
2.2.1 Member inner class
Modifier class class name {
Modifier class class name {
//…
}
//…
}
Note: For internal use, you can access it at will; for external use, you need internal class objects.
How to use member inner classes? There are two ways:
1. Indirect method: use the inner class among the methods of the outer class, and then main just calls the method of the outer class
2. Direct method:
External class name.Inner class name Object name=new External class name().new Inner class name();
//External class public class Body { //Member inner class public class Heart{ //inner class method public void beat(){ System.out.println("Heart beating: bouncing"); System.out.println("My name" + name);//Correct writing method } } //External class member variables private String name; public void methodBody(){ System.out.println("Methods of external classes"); new Heart().beat(); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
public class MyClass { public static void main(String[] args) { //indirect call Body body=new Body();//Object of external class body.setName("vivi"); //Call the method of the external class through the object of the external class, and directly use the internal class heart body.methodBody(); //call directly Body.Heart heart=new Body().new Heart(); heart.beat(); } }
How to access the internal class method of a member with the same name:
External class name.this.External class member variable name
public class Outer { int num=10; //Member variables of external class public class Inner{ int num=20;//Member variables of the inner class public void methodInner(){ int num=30;//Local variables of internal class methods System.out.println(num);//Local variables, proximity principle System.out.println(this.num);//Member variables of internal classes // System.out.println(super.num);//No, there is no inheritance relationship System.out.println(Outer.this.num);//Member variables of external classes } } }
public class MyClass2 { public static void main(String[] args) { Outer.Inner inner=new Outer().new Inner(); inner.methodInner(); } }
2.2.2 Local inner classes (including anonymous inner classes)
If a class is defined inside a method, then it is a local inner class.
“Partial” can only be used by the method it currently belongs to. It cannot be used outside this method.
Modifier class external class name {
Modifier Return value type External class method name (parameter list) {
class local internal class name {
//…
}
}
}
public class Outer { public void methodOuter(){ class Inner{ int num=10; public void methodInner(){ System.out.println(num);//10 } } Inner inner=new Inner(); inner.methodInner(); } }
public class Main { public static void main(String[] args) { Outer outer=new Outer(); outer.methodOuter(); } }
Summarize the permission modifiers of classes:
public > protected > (default) > private
When defining a class, permission modifier rules:
1. External class: public/(default)
2. Member inner class: public/protected/(default)/private
3. Local inner classes: nothing can be written
Note:
For local inner classes, if you want to access the local variables of the method, then this local variable must be [effectively final]
Remarks: Starting from Java8, as long as the local variables remain unchanged, the final keyword can be omitted.
Reason:
1. The object produced by new is in the heap memory room.
2. Local variables follow the method and are in the stack memory.
3. After the method is finished running, pop it off the stack immediately, and the local variables will disappear immediately.
4. But the new object will continue to exist in the heap until the garbage collection disappears.
public class MyOuter { public void methodOutrer(){ int num=10; class MyInner{ public void methodInner(){ System.out.println(num); } } } }
2.2.3 Anonymous inner classes
If the implementation class of the interface (or a subclass of the parent class) only needs to be used once, then in this case you can omit the definition of the class and use [anonymous inner class] instead.
Interface name object name = new interface name () {
//Override and rewrite all abstract methods
};
public interface MyInterface { void method();//Abstract method }
public class MyInterfaceImpl implements MyInterface{ @Override public void method() { System.out.println("The implementation class overrides the method"); } }
public class DemoMain { public static void main(String[] args) { // MyInterfaceImpl impl=new MyInterfaceImpl(); // impl.method(); //Use anonymous inner class MyInterface myInterface=new MyInterface() { @Override public void method() { System.out.println("Overriding the method method"); } }; myInterface.method(); } }
Parse the “new interface name(){…}” format:
1.new represents the action of creating an object
2. The interface name is which interface the anonymous inner class needs to implement
3.{…}This is the content of the anonymous inner class
Note:
1. Anonymous inner classes can only be used once when [creating an object]
If you want to create an object multiple times and the content of the class is the same, you must use a separately defined implementation class.
2. Anonymous objects, when [calling methods], you call them only once
If you want to call the same object multiple times, you must give the object a name.
3. The anonymous inner class omits the [implementation class/subclass], but the anonymous object omits the [object name]
Emphasis: Anonymous inner classes and anonymous objects are not the same thing!
public interface MyInterface { void method1();//Abstract method void method2();//Abstract method }
public class MyInterfaceImpl implements MyInterface{ @Override public void method1() { System.out.println("The implementation class overrides method 1"); } @Override public void method2() { System.out.println("The implementation class overrides method 2"); } }
public class DemoMain { public static void main(String[] args) { // MyInterfaceImpl impl=new MyInterfaceImpl(); // impl.method(); //Use an anonymous inner class, but not an anonymous object. The object name is called myInterface. MyInterface myInterface = new MyInterface() { @Override public void method1() { System.out.println("Overriding method 1"); } @Override public void method2() { System.out.println("Overriding method 2"); } }; myInterface.method1(); //Use an anonymous inner class, and omit the object name, which is also an anonymous object new MyInterface() { @Override public void method1() { System.out.println("Overriding method 1"); } @Override public void method2() { System.out.println("Overriding method 2"); } }; myInterface.method1(); //Because anonymous objects cannot call the second method, you need to create an anonymous object of an anonymous inner class } }
3 Others
3.1 Class as member variable type
public class Hero { private String name;//hero name private Weapom weapom;//Hero’s weapon private int age;//Hero’s age public Hero(String name) { this.name = name; } public Hero() { } public void attack(){ System.out.println("The age is" + age + "" + name + "is using" + weapom.getCode() + "attacking the enemy"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Weapom getWeapom() { return weapom; } public void setWeapom(Weapom weapom) { this.weapom = weapom; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Weapom { private String code;//weapon code public Weapom(String code) { this.code = code; } publicWeapon() { } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
public class DemoMain { public static void main(String[] args) { //Create a hero character Hero hero=new Hero(); //Give the hero a name and set the age hero.setName("王五"); hero.setAge(22); //Equip weapons for heroes Weapom weapom=new Weapom("knife"); hero.setWeapom(weapom); hero.attack(); } }
3.2 Interface as member variable type
public interface Skill { void use();//Abstract method to release skills }
public class SkillImpl implements Skill{ @Override public void use() { System.out.println("咻咻咻--"); } }
public class Hero { private String name;//Hero’s name private Skill skill;//Hero’s spell skills public Hero(String name, Skill skill) { this.name = name; this.skill = skill; } public Hero() { } public void attack(){ System.out.println("My name is" + name + ", start releasing skills:"); skill.use(); System.out.println("Skill release completed"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public Skill getSkill() { return skill; } public void setSkill(Skill skill) { this.skill = skill; } }
public class DemoGame { public static void main(String[] args) { Hero hero=new Hero(); hero.setName("cici");//Set the name of the hero System.out.println(hero.getName()); //Set hero skills // hero.setSkill(new SkillImpl());//Use a separately defined implementation class //You can also use anonymous inner classes // Skill skill=new Skill() { // @Override // public void use() { // System.out.println("Xiuxiuxiu_"); // } // }; // hero.setSkill(skill); //Further simplified, use anonymous inner classes and anonymous objects at the same time hero.setSkill(new Skill() { @Override public void use() { System.out.println("hahaha_"); } }); hero.attack(); } }
3.3 Interface as parameter or return value of method
/** * java.util.list is the interface implemented by ArrayList */ public class DemoInterface { public static void main(String[] args) { //The left side is the interface name, and the right side is the implementation class name. This is polymorphic writing. List<String> list=new ArrayList<>(); System.out.println(addNames(list)); } public static List<String> addNames(List<String> list){ list.add("cici"); list.add("asjkf"); list.add("sdkjha"); return list; } }
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeBasic syntax of JavaKeywords in Java 138625 people are learning the system