One: Access modifier
Each member of a class, including member variables, methods, and constructors, has an access modifier that is used to determine who can access the member. Java provides four access levels for members in a class, arranged in the following order of access from large to small:
1. Open access level: Use the public keyword modifier. Members that are decorated with the public keyword are exposed externally, that is, public members can be accessed by any other object.
2. Protected Access level: Use the protected keyword modifier. Protected members can be accessed by classes in the same package, and can also be accessed by subclasses of the class, regardless of which package the subclass is in.
3. Default Access level: No access modifier. Members of the default access level can be accessed by other classes in the same package.
4. Private access level: decorated with the private keyword. It is the lowest level in the four access modifiers. Private members are accessible only to the class itself, not to the public.
public--the class or non-class is accessible
private--only this class can access
protected--members of the class and its subclasses are accessible, and the classes in the same package are accessible
Default--classes in the same packet can be accessed
Two: Package
The purpose of the access modifier is to limit the range of properties and methods that are used and called. That indicates the degree to which the data members and methods of the class can be accessed. If a class's component (data member or method) is declared private, any method outside of that class cannot access the component. Get and Set methods: Getxxxx ()----accessor setxxxx ()----Modifier Encapsulation Benefits: member variables of a class can be read-only or write-only. For example, the member variable ID in the Salesperson class is read-only, and the ID member variable cannot be changed after the salesperson object is instantiated. A class can have a holistic control over what is stored in its member variables. For example, the value of member variable commissionrate in the Salesperson class can only be between 0.0 and 0.20. The user of the class does not need to know how the class stores the data. A class can change the data type of a member variable, and the user of the class does not need to change its code.
Three: Static class membersThe static modifier's global variable is called a statically variable, also called a class variable
The value of the class variable is shared by all objects
The static method is called a statically-modified class method.
Class methods can use only static variables, not non-static global variables
The use of class members (class variables and class methods) does not require an instance to be created by:
ClassName. Direct reference to the form of variablename
Attention:
Static methods cannot access static members without accessing non-static members
Four: static initialization blockIn addition to declaring static member variables and methods, there is another way to use the keyword static. A Java class can contain a static initialization block, a static initialization block that is a set of statements that are executed when the class is loaded by the JVM's class loader. Shaped like:
public class {
static{
Statement
}
}
V: Instance initialization block
An instance initialization block is similar to a static initialization block, which executes once every time that an object of a class is instantiated. The difference between the instance initialization block and the constructor is that the instance initialization block executes before the constructor call. Shaped like:
public class {
{
Statement
}
}
The statements in the instance initialization block are executed before the subclass constructor call, after any parent class constructor call. When an object is instantiated and the class contains an instance initialization block, the following events occur sequentially:
1. The corresponding constructor in the subclass is called.
2, to perform the call to Super, the control process jumps to the corresponding parent class constructor.
3, the parent class constructor finishes execution, the control flow back to the subclass constructor.
4. The instance initialization block executes before any statements after super () in the subclass constructor are executed.
5. Finally, execute the statement after Super () in the subclass constructor.
VI: Inner class
In Java, you can also define classes within a class. This class, defined inside a class, is called an inner class. The class in which the inner class resides is called an external class. Characteristics:
A separate class file
also need to compile
can also produce objects
Learning to use internal classes is part of Mastering Java Advanced Programming, which allows you to design your program structure more elegantly
Inner class--member inner class--Static inner class
Inner class--local inner class--Anonymous inner class
member Inner class:
Member inner classes are defined in the following way:
Class Outer {
Class Inner {
}
}
You can use an object of the inner class only after you have created an instance object of the outer class. Static inner classes: Static inner classes are defined in the following way:
Class Outer {
Static Class Inner {
}
}
The inner class exists inside the outer class as a static member of the outer. Because static members can be used without creating a class, we can not create an object of the outer class and directly reference the inner class. Local inner class: a local inner class is a class that is defined inside a method of a class. A local inner class can only be used inside a method. Once the method is executed, the local inner class is purged from memory. Anonymous inner class: the anonymous inner class is a special inner class that does not have a name. The definition of an anonymous inner class is combined with the creation of an object, and the entire action looks like a production object. Anonymous inner class is a class that is defined for a unique object
Advanced Concepts for classes