To learn Java, you must understand constructors. Because constructors can provide many special methods, this is often confusing for beginners. However, there are important differences between constructors and methods. By Robert Nielsen www.javaworld.com
We say that the constructor is a method, just as we say that the Australian platypus is a mammal. To understand the platypus, one must first understand how it differs from other mammals. Likewise, to understand constructors, it is important to understand the difference between constructors and methods. It is important for anyone learning Java, especially those taking the certification exam, to understand the constructor. The following is a brief introduction, with a table to make some simple summaries.
Different constructors of functions and functions are used to create an instance of a class. This process can also be used when creating an object:
Platypus p1 = new Platypus();
Copy the code
Instead, methods are used to execute Java code.
The different constructors and methods for ## modifiers, return values, and names are distinguished in three convenient ways: modifiers, return values, and names. As with methods, constructors can have any accessable modifier: public, protected, private, or no modifier (usually called by package and friendly). Unlike methods, constructors cannot have the following non-access modifications: abstract, final, native, static, or synchronized.
The return type is also very important. Methods can return any type of value or no value (void). The constructor does not return a value and does not require void.
Finally, the naming of both. The constructor uses the same name as the class, but the methods are different. By convention, methods usually start with a lowercase letter, while constructors usually start with a capital letter. The constructor is usually a noun because it is the same as the class name; Methods are usually closer to verbs because they describe an action.
There is a big difference between the use of the constructor and the use of the this keyword in methods. Method references this to an instance of the class executing the method. Static methods cannot use this because static methods do not belong to an instance of the class, so this has nothing to point to. The constructor’s this points to another constructor with a different argument list in the same class. Let’s look at the following code:
public class Platypus { String name; Platypus(String input) { name = input; } Platypus() { this("John/Mary Doe"); } public static void main(String args[]) { Platypus p1 = new Platypus("digger"); Platypus p2 = new Platypus(); }}Copy the code
In the code above, there are two constructors for different argument lists. The first constructor assigns a value to the member name of the class. The second constructor calls the first constructor and assigns an initial value of “John/Mary Doe” to the member variable name.
In the constructor, if the keyword this is used, it must be placed on the first line, otherwise it will result in a compilation error.
## the usage constructor and method of super, both use the keyword super to refer to the superclass, but use different methods. Method uses this keyword to execute a method in an overloaded superclass. Look at the following example:
class Mammal { void getBirthInfo() { System.out.println("born alive."); } } class Platypus extends Mammal { void getBirthInfo() { System.out.println("hatch from eggs"); System.out.print("a mammal normally is "); super.getBirthInfo(); }}Copy the code
In the example above, use super.getbirthInfo () to call the overloaded method in the superclass Mammal.
The constructor uses super to call the constructor in the superclass. And this line of code must be on the first line, or the compilation will fail. Look at the following example:
public class SuperClassDemo { SuperClassDemo() {} } class Child extends SuperClassDemo { Child() { super(); }}Copy the code
In the moot example above, the constructor Child() contains super, which instantiates the superclass constructor SuperClassDemo and adds it to the Child class.
The compiler automatically adds code to the constructor, which may be confusing to novice Java programmers. When we write a class with no constructor and compile it, the compiler automatically adds a constructor with no arguments, for example:
public class Example {}
Copy the code
The following code will be compiled:
public class Example {
Example() {}
}
Copy the code
On the first line of the constructor, if super is not used, the compiler automatically adds, for example:
public class TestConstructors {
TestConstructors() {}
}
Copy the code
The compiler adds code like this:
public class TestConstructors { TestConstructors() { super; }}Copy the code
If you think about it, you know the code below
public class Example {}
Copy the code
The code will be added by the compiler like this:
public class Example { Example() { super; }}Copy the code
# # inheritance
Constructors cannot be inherited. A subclass can inherit any method of a superclass. Look at the following code:
public class Example {
public void sayHi {
system.out.println("Hi");
}
Example() {}
}
public class SubClass extends Example {
}
Copy the code
SubClass automatically inherits the sayHi method from its parent, but the constructor Example() cannot be inherited.
# # to summarize
The theme | structure | methods |
---|---|---|
function | Create an instance of the class | Java Function statements |
modified | Can’t usebstract .final .native .static .or synchronized |
can |
The return type | No return value, no void | It has a return value, or void |
named | Same as the class name; Usually a noun, beginning with a capital letter | Usually represents the meaning of a verb, beginning with a lower case |
this | Points to another constructor in the same class, in the first line | Refers to an instance of the current class and cannot be used for static methods |
super | Calls the parent class’s constructor, on the first line | Calls an overloaded method in the parent class |
inheritance | The constructor cannot be inherited | Methods can be inherited |
The compiler automatically adds a default constructor | Auto join (if not) | Does not support |
The compiler automatically adds a default call to the constructor of the superclass | Auto join (if not) | Does not support |