Problems with the Java access modifier constructor
Java access Modifier-Qualifier Summary (analogy C #)
Java access modifier
----------
[Public | default | protected | private]
Public: Fully developed
Protected: Same package and sub-class access
Default (None): Same Package access
Private: can only be accessed in this class
JAVA is used for class modifiers (2) and Qualifiers (2):
---------------
Public: Fully developed
Default (None): Same Package access
Abstract: Specified as an abstraction class
Final: Specified as the final class (Eunuch class, Sealed Class), no sub-class
JAVA modifiers for member variables:
---------------------
[Public | protected | private] [Static] [Final] [Transient] [volatile] int i;
Qualifier Meaning:
Static: Class properties (C # static fields), which can be called directly through the class name
Final: The member cannot be overridden, that is, defining constants (equivalent to the use of Const in C #)
Transient: not serialized
Volatile: Can be accessed by multiple threads (synchronous variable)
JAVA modifiers for member methods:
----------------------
[Public | protected | private] [Static] [Final| Abstract] [Native] [Synchronized] Method (int i) {}
Qualifier Meaning:
Static: Class (Static) method that can be called directly from the class name
Final: Method cannot be overridden (similar to sealed sealing method in C #)
Native: Code-local methods for integrating other languages
Abstract: Abstraction method, no method body
Synchronized: Controlling access to multiple concurrent threads (synchronous method)
In Java, both explicit and implicit require that the constructor of its parent class be called in the constructor of the subclass.
If the parent class has no constructor (in fact, a default parameterless constructor), the constructor of the subclass is called automatically, and if the parent class has its own constructor (the parent class does not have a default parameterless constructor), then in the subclass constructor, you must call a constructor of the parent class. and must be called in the first statement of the construction method. The reason is that the Java language designer must be the responsibility of the subclass to ensure that it inherits the parent class as soon as possible into a stable, complete state. Imagine, without this constraint, that a method of a subclass that inherits from the parent class might use some of the variables in the parent class, and those variables are not initialized, which can have some unpredictable consequences.
1 The constructor must satisfy the syntax rule that the method name must be the same as the class name, do not declare the return type, and cannot be modified by static, final, synchronized, abstract, native, and so on.
2 constructor methods can be overloaded to express the various initialization behaviors of an object. The This statement can be used in overloaded construction methods to invoke other construction methods, and it should be noted that if you use the this statement in a constructed method, you must act as the first statement of the constructed method, and you can invoke the other constructor of the class with the this statement only in one of the constructor methods. Instead of calling the constructor of a class in a member method, you can call other construction methods only through the this statement, and you cannot call the constructor directly through the method name.
3 in Java, if no construction method is provided in a user-defined class, the Java language automatically provides a default constructor method that has no parameters, is decorated with public, and the method body is empty. Of course, the program can also display the definition of the default constructor method.
4 If the class shows that one or more construction methods are defined, and all of the constructor methods have parameters, then the class loses the default constructor that the Java language prepares for, and when the default constructor method for that class is used (that is, the constructor without parameters) is a compilation error.
5 The constructor method of the parent class cannot inherit from the quilt class, but the constructor of the parent class can be called through the super statement in the constructor method of the subclass. Using the Super statement should be aware that the Super statement is used in the constructor of a subclass and it must act as the first statement.
6 When you create an object of a subclass, the Java Virtual machine first executes the constructor of the parent class, and then executes the constructor of the subclass, and in the case of multi-level inheritance, starts from the topmost parent of the inheritance tree, executes the constructor of each class in turn, This guarantees that the instance variables inherited from all direct or indirect parent classes are properly initialized by the child class object.
7 if the constructor of the subclass is not shown in the construction method of the child class using the Super statement to display the constructor of the calling parent class, the Java virtual opportunity will automatically first invoke the default constructor of the parent class if the parent class does not have a default constructor method when the child class object is created by this constructor method. For example
public class B extends a{
B (int i) {//The constructor method does not have a super display calling the constructor of the parent class, the default constructor method of the parent class is called automatically, and the parent class A does not have a default constructor method, which compiles the error
System.out.println (i);
}
public static void Main (string[] args) {
b b=new B (2);
}
}
Class a{//a constructor with parameters is defined, and the default parameterless construction method is lost.
int i;
A (int i) {
this.i=i*2;
}
}
8 constructor methods are called in several ways: the other constructor methods of the current class are called by the This statement;
The constructor of the subclass of the current class is called with a super statement, and it is called through the new statement in the program.
9 The construction method can be in public, protected, private, and one of the default four access levels. When the constructor method is private, it means that it can only be accessed in the current class, cannot be inherited, and cannot be created by other programs with new instance objects. You can compare the effects of several other modifiers: the abstract decorated class, which is not allowed to be instantiated, is the same as the private modifier construction method, but the abstract decorated class can be inherited, has subclasses, can create instances of subclasses, and final class is forbidden to inherit, This is the same as the private modifier construction method, but the final class can create an instance object with new.
Turn from:http://blog.csdn.net/luyuncsd123/article/details/8198910
Problems with the Java Access Modifier constructor (GO)