Home >Java >javaTutorial >Detailed explanation of the use of constructors in Java
This article mainly introduces relevant information on how to use Java constructors and precautions. Here are examples of how to use constructors and what needs to be paid attention to. Friends in need can refer to
Java Constructor usage methods and precautions
The constructor of the super class is run before the constructor of the subclass is run. That is to say, when creating an object, the constructor in the super class is run first, and then Then run the constructor in the subclass. At this time, if the constructor executes an overridable method when executing the superclass constructor, then the method in the subclass will be called, and the subclass has not yet is instantiated, problems may occur at this time.
Take an example to illustrate:
public class Super { int age = 10; protected void say() { System.out.println("super"); } public Super() { override(); } public void override() { System.out.println("super override"); } } class Sub extends Super { int age = 9; private final Date date; public Sub() { date = new Date(); } @Override public void override() { System.out.println(date); } @Override protected void say() { System.out.println("sub"); } public static void main(String[] args) { Super sub = new Sub(); sub.override(); // sub.say(); // System.out.println(sub.age); } }
Create two classes, one Super.java, in which a member variable age= is defined. 10, and the override() method is called in the constructor. Sub.java inherits Super.java and initializes date in the constructor.
Now we execute the main method, what will be printed on the console? Is it one date? Or two dates? The answer is to print out null first, and then print out the specific date value. And if any method of date is called in the override method of Sub.java, NPE
Why does this happen?
We know that when using new to create an object, we will first check whether the class inherits from the parent class. If so, the constructor of the parent class will be called first. In this case, in new When calling Sub(), it was found that Sub is a subclass of Super, so the constructor of Super was called first. In the constructor of Super, it was found that the override method was called, so the subclass Sub was found to have overridden the override method and found that the subclass If the class is rewritten, the override of the subclass is called to print out the date. At this time, the date has not been initialized, so the value is null.
After the construction method of the parent class is executed, the construction method of the subclass is executed. At this time, the date is initialized, so when the override method is called again, the date will be printed correctly.
When calling sub.age, what will be printed? Is it 10 or 9?
actually prints out 10. When creating an object, there are two types of instance variables, the first is the declared type, and the second is the actual type
## The declared type of #
Super sub = new Sub();sub is Super, and the actual type is Sub.
The above is the detailed content of Detailed explanation of the use of constructors in Java. For more information, please follow other related articles on the PHP Chinese website!