A, keywords: static
1- Design ideas
Class attributes are shared variables between objects of the class. When designing a class, analyze which properties do not change from object to object and set those properties as class properties. The corresponding method is set to a class method.
If a method is independent of the caller, such a method is usually declared as a class method, which simplifies method invocation because it can be called without creating an object.
2- Scope of use
In Java classes, static modifies properties, methods, code blocks, inner classes
3- The decorated member has the following characteristics
Loads as the class loads
Exists prior to the object
Decorated member shared by all objects
When the access permission permits, the object is not created and is directly called by the class
The class name can be used when there is no instance of the object. The form of the method name () accesses class methods decorated with static.
You can only access static modified properties or methods of a class inside a static method. You cannot access non-static structures of a class.
Because static methods can be accessed without an instance, static methods cannot have this or super inside them, and static methods cannot be overridden
4- Singleton design pattern
It is to take certain methods to ensure that in the whole software system, there can only be one object instance for a certain class, and the class only provides a method to obtain its object instance. If we want a class to produce only one object in a virtual machine, we must first set access to the constructor of the class to private, so that we cannot use the new operator to produce objects of the class outside the class, but can still produce objects of the class inside the class. Since you can’t get an object of the class from outside the class, you can only call a static method of the class to return an object created inside the class. Static methods can only access static member variables of the class. Therefore, variables that point to objects of the class generated inside the class must also be defined as static.
The private constructor –> internally provides an instance of the current class –> this instance must also be static –> provides a public static method that returns an object of the current class
Advantages: The singleton mode generates only one instance, reducing the system performance overhead. When more resources are needed to generate an object, such as reading configurations or generating other dependent objects, the singleton can be generated directly when the application is started and then permanently reside in the memory
Understand the syntax of the main method
Since the Java virtual machine calls the main() method of the class, the method’s access must be public, and since the Java virtual machine does not have to create objects when executing the main() method, the method must be static. The method takes a String array argument, This array holds the arguments that are passed to the class being run when the Java command is executed.
Since the main() method is static, we cannot access the non-static members of the class directly. We must create an instance object of the class to access the non-static members of the class, as we have seen several times in previous examples
Class member 4: code block
1 – role
Initialize a Java class or object
Classification of 2 –
Code blocks in a class that have modifiers can only be static blocks. Code blocks that do not use static modifiers are non-static code blocks.
Static code blocks are usually used to initialize static properties
3- Static code blocks
You can have output statements.
You can initialize class attributes and class declarations.
Non-static attributes cannot be initialized. That is, you cannot call non-static properties and methods.
If there are multiple static blocks of code, execute them from top to bottom.
Static code blocks are executed before non-static code blocks.
Static code blocks are loaded as the class is loaded and executed only once.
4- Non-static code blocks
You can have output statements.
You can initialize class attributes and class declarations.
In addition to calling non-static structures, you can also call static variables or methods.
If you have multiple non-static blocks of code, execute them from top to bottom.
This is done each time an object is created. And executes before the constructor.
5- The order in which member variable assignments are executed in a program
Declare the default initialization of a member variable
Explicit initialization, multiple initialization blocks executed in sequence (in sequence at the same level)
The constructor then initializes the member
By object. Property or object. Method can be assigned multiple times
Four, key words: final
When declaring classes, variables, and methods in Java, you can use the keyword final to denote “final.”
Classes with final tags cannot be inherited. Improve security and readability of programs. String class, System class, StringBuffer class
Methods of final tags cannot be overridden by subclasses. Such as getClass() in the Object class.
Variables (member variables or local variables) tagged with a final tag are called constants. The name is uppercase and can only be assigned once.
Member variables of final tags must be explicitly assigned at declaration time or in each constructor or code block before they can be used.
Abstract classes and methods
The abstract keyword is used to modify a class called an abstract class.
The use of abstract modifies a method called abstract method. Abstract method: only method declaration, no method implementation. End with a semicolon: for example: public abstract void talk();
Classes with abstract methods must be declared as abstract classes.
Abstract classes cannot be instantiated. Abstract classes are intended to be inherited, and subclasses of the abstract class must override the parent class’s abstract methods and provide the method body. If you have not overridden all of the abstract methods, the class remains abstract.
You cannot use abstract to modify variables, code blocks, and constructors.
You cannot use abstract to modify private methods, static methods, final methods, or final classes.
Abstract classes are classes that model objects whose parent class cannot determine the full implementation, but whose subclasses provide concrete implementation.
TemplateMethod design pattern (TemplateMethod) : abstract class is a kind of template pattern design, abstract class as a general template of multiple subclasses, subclasses extend and transform on the basis of abstract class, but subclasses generally retain the behavior of abstract class.
6. Interface
Definition 1 –
On the one hand, it is sometimes necessary to derive a subclass from several classes, inheriting all of their properties and methods. However, Java does not support multiple inheritance. With interfaces, you get the effect of multiple inheritance. On the other hand, it is sometimes necessary to extract some common behavior characteristics from several classes that do not have an IS-A relationship, but simply share the same behavior characteristics.
An interface is a specification that defines a set of rules that represent the real world “if you are/want to… Must be able to…” Thoughts. Inheritance is a “no” relationship, whereas interface implementation is a “no” relationship. Interfaces are essentially contracts, standards, specifications, just like our laws. After the rules are made, everyone must abide by them
An interface is a collection of abstract methods and constant value definitions.
Characteristics of 2 –
Let’s call it interface.
All member variables in an interface are decorated with public static final by default.
All abstract methods in an interface are qualified by public Abstract by default.
There is no constructor in the interface.
Interfaces adopt multiple inheritance mechanism.
3- Syntax format
Implements SubClass extends SuperClass implements InterfaceA{}
A class can implement multiple interfaces, and interfaces can inherit from other interfaces.
The class that implements the interface must provide concrete implementation content for all methods in the interface to be instantiated. Otherwise, it remains abstract.
The main purpose of an interface is to be implemented by an implementation class. (Interface oriented programming)
Similar to inheritance relationships, there is polymorphism between interfaces and implementation classes
Interfaces and classes are juxtaposed, or can be understood as a special kind of class. In essence, an interface is a special abstract class that contains only definitions of constants and methods (JDK7.0 and earlier), but no implementations of variables and methods.
4- Comparison between interfaces and abstract classes
In development, it is common to see a class that, instead of inheriting an already implemented class, either inherits an abstract class or implements an interface.
Member of class 5: inner class
Definition 1 –
When there is an internal part of a thing that needs to be described by a complete structure that only serves the external thing, then the entire internal complete structure is best used by the inner class.
In Java, the definition of one class is allowed to reside inside another class, called an inner class and an outer class.
An Inner class is usually used within the class or block in which it is defined, and must be given a full name when referenced externally.
The name of an Inner class cannot be the same as the name of the outer class containing it;
Classification of 2 –
Member inner classes (static member inner classes and non-static member inner classes)
Local inner classes (not to mention modifiers), anonymous inner classes
3- Member inner class’s role as a member of the class
Unlike external classes, Inner classes can also be declared private or protected;
The structure of an external class can be invoked
Inner classes can be declared static, but non-static member variables of outer classes cannot be used.
4- The role of the member inner class as a class
You can define properties, methods, constructors, and so on internally
Can be declared as an abstract class and therefore can be inherited by other inner classes
Can be declared final
OuterClass$innerClass. class bytecode file (also available for local inner classes)
5 – note
A member of a non-static member inner class cannot be declared static. A member can be declared static only in an outer class or a static member inner class.
An external class accessing a member of an inner class requires an inner class. Member or inner class object. Members”
Member inner classes can directly use all members of the external class, including private data
When you want to use an inner class for the static member part of an outer class, consider declaring the inner class static
6- How do I declare a local inner class
7- How do I use local inner classes
Use it only in the method or block of code that declares it, and use it first and then. This class cannot be used anywhere else, but its objects can be returned from the return value of an external method. The return value type can only be the parent or parent interface type of a local inner class
8- Local inner class features
The inner class is still a separate class. After compilation, the inner class is compiled into a separate.class file, but it is preceded by the outer class’s class name, $symbol, and numeric number.
Use it only in the method or block of code that declares it, and use it first and then. You can’t use this class anywhere else.
Local inner classes can use members of external classes, including private ones.
Local inner classes can use local variables of external methods, but they must be final. By local inner class and local variable declaration cycle is different.
Local inner classes have similar status to local variables and cannot use public,protected, default, or private
Local inner classes cannot use static decorations and therefore cannot contain static members
9- Anonymous inner classes
You cannot define any static members, methods, or classes. You can only create an instance of an anonymous inner class. An anonymous inner class must be used after new to implicitly implement an interface or implement a class.
Format: new superclass constructor (argument list) | implementing an interface () {/ / class body part of the anonymous inner class}
Features: Anonymous inner classes must inherit from parent classes or implement interfaces
Anonymous inner classes can have only one object
Anonymous inner class objects can only be referenced in polymorphic form