Constructor
Differences between constructors and Methods
Summary
To learn Java, you must understand the constructor. The constructor provides many special methods, which are often confusing for beginners. However, there are many important differences between constructors and methods.
Author: Robert Nielsen Origin Site: www.javaworld.com
Let's say that the constructor is a way, just like the Australian duckbill is a kind of feeding animal. (According to: when foreigners like to give a metaphor, I will also follow the translation ). To understand the duckbill, you must first understand the difference between it and other feeding animals. Similarly, to understand the constructor, you must understand the difference between the constructor and the method. All Java learners, especially those who want to take certification exams, are very important to understand the constructor. The following is a brief introduction. At last, a table is used to make some simple summary.
Different functions and functions
The constructor is used to create an instance of a class. This process can also be used when creating an object: Platypus p1 = new platypus ();
Instead, methods are used to execute Java code.
Modifier. the return value and name are different.
There are three convenient differences between constructors and Methods: modifiers, return values, and names. Like the method, the constructor can have any access modifier: public, protected, private, or not modified (usually called by package and friendly ). unlike methods, constructors cannot have the following non-access modifiers: abstract, final, native, static, or synchronized.
The return type is also very important. The method can return any type of value or no return value (void). The constructor does not return a value or require void.
Finally, let's talk about the naming of the two. The constructor uses the same name as the class, while the method is different. According to the habit, the method usually starts with a lower-case letter, while the constructor usually starts with a upper-case letter. The constructor is usually a noun because it is the same as the class name, and the method is usually closer to the verb because it indicates an operation.
Usage of "this"
The constructors and methods use the keyword "this. The method references this to the instance of the class that is executing the method. Static methods cannot use the this keyword. Because static methods do not belong to class instances, this has nothing to point. This Of the constructor points to another constructor in the same class with different parameter lists. 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 ();
}
}
In the above Code, there are two constructors with different parameter lists. The first constructor assigns a value to the member name of the class, the second constructor calls the first constructor, and gives the member variable name an initial value "John/Mary doe ".
In the constructor, if you want to use the keyword this, it must be placed in the first line. Otherwise, a compilation error will occur.
Usage of "super"
Constructors and methods use the keyword "super" to point to the superclass, but the methods used are different. The method uses this keyword to execute the methods in the overloaded superclass. See 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 ();
}
}
In the preceding example, super. getbirthinfo () is used to call the overloaded method in the super class mammal.
The constructor uses super to call the constructor in the superclass. In addition, this line of code must be placed in the first line, otherwise compilation will fail. See the following example:
Public class superclassdemo {
Superclassdemo (){}
}
Class Child extends superclassdemo {
Child (){
Super ();
}
}
In the above example, the constructor child () contains super, which instantiates the constructor superclassdemo in the superclass and adds it to the Child class.
The compiler automatically adds code
The compiler automatically adds code to the constructor. For this reason, new Java programmers may be confused. When we write a class without a constructor and compile it, the compiler automatically adds a constructor without parameters, for example, public class example {}
Compile the following code:
Public class example {
Example (){}
}
In the first line of the constructor, if super is not used, the compiler will automatically add it. For example:
Public class testconstructors {
Testconstructors (){}
}
The compiler adds the following code:
Public class testconstructors {
Testconstructors (){
Super;
}
}
Think about the code below.
Public class example {}
The code will be added by the compiler as follows:
Public class example {
Example (){
Super;
}
}
Inheritance
The constructor cannot be inherited. Sub-classes can inherit any methods of superclasses. Take a look at the following code:
Public class example {
Public void sayhi {
System. Out. println ("hi ");
}
Example (){}
}
Public class subclass extends example {
}
The class subclass automatically inherits the sayhi method in the parent class, but the constructor example () in the parent class cannot be inherited.
Summary
Topic |
Constructor |
Method |
Function |
Create a class instance |
Java functional statements |
Modify |
Unavailablebstract ,final ,native ,static , Orsynchronized |
Yes
|
Return type |
No return value, no void
|
Return value or void |
Name |
It is the same as the class name. It is usually a noun and starts with an uppercase letter. |
It usually indicates the meaning of a verb, starting with lowercase. |
this |
Point to another constructor in the same class, in the first line |
Pointing to an instance of the current class and cannot be used for static methods |
super |
Call the constructor of the parent class, in the first line |
Call an overloaded method in the parent class |
Inheritance |
The constructor cannot be inherited. |
Methods can be inherited |
The compiler automatically adds a default constructor. |
Automatically add (if not) |
Not Supported |
The compiler automatically adds a default caller to the hyperclass constructor. |
Automatically add (if not) |
Not Supported |