The difference between constructors and methods
Summary
To learn Java, you have to understand the constructor. Because constructors can provide a number of special methods, this is often confusing for beginners. However, there are many important differences between the constructor and the method.
Original Author: Robert Nielsen original station: www.javaworld.com
We say the builder is a method, like the Australian platypus is a feeding animal. (by: Foreigners like to play metaphor, I also follow the translation). To understand the platypus, you must first understand the difference between it and other animal-feeding animals. Similarly, to understand the constructor, you need to understand the difference between the constructor and the method. It is very important for all Java learners, especially those who want to authenticate the exam, to understand the builder. Here will be a brief introduction, and finally use a table for some simple summary.
The difference between function and effect
Constructors are created to create an instance of a class. This process can also be used when creating an object: Platypus p1 = new Platypus ();
Instead, the purpose of the method is to execute Java code.
modifiers, returning values and naming differences
There are three convenient differences between constructors and methods: modifiers, return values, naming. As with methods, constructors can have any access modifiers: public, protected, private, or not decorated (usually called by package and friendly). Unlike the method, the constructor 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 has no return value, and no void is required.
Finally, talk about the naming of both. Constructors use the same name as the class, but the method is different. By custom, methods usually start with lowercase letters, and constructors usually start with uppercase letters. A 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 describes an action.
Use of "this"
Constructors and methods use the keyword this is a big difference. Method references this to an instance of the class that is executing the method. Static methods cannot use the This keyword, because static methods do not belong to instances of the class, so there is nothing to point to. This point in the constructor points to another constructor in the same class with a different argument list, and we 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 code above, there are 2 constructors for different parameter lists. The first constructor assigns a value to the member name of the class, the second constructor, invokes the first constructor, and gives the member variable name an initial value of "John/mary Doe".
In the constructor, if you want to use the keyword this, you must put it in the first row, and if not, it will cause a compilation error.
The use of "super"
Constructors and methods use the keyword super to point to a superclass, but the method is different. method to execute the method in the overloaded superclass using this keyword. Look at the following example:
In the above example, the Super.getbirthinfo () is used to invoke the overloaded method in the superclass mammal.
The constructor uses super to invoke the constructor in the superclass. And this line of code must be placed on the first line, otherwise the compilation will go wrong. Look at the following example:
public class Superclassdemo {
Superclassdemo () {}
}
Class Child extends Superclassdemo {
Child () {
Super ();
}
}
In the example above that has no practical meaning, the constructor child () contains super, which is the role of instantiating the constructor Superclassdemo in the superclass and adding it to the child class.
compiler automatically joins code
The compiler automatically adds code to the constructor, and for this, novice Java programmers may be confused. When we write a class that does not have a constructor, the compiler automatically adds a constructor with no arguments, for example: public class Example {}
The following code is compiled:
public class Example {
Example () {}
}
In the first line of the constructor, no super is used, and the compiler is automatically added, for example:
public class Testconstructors {
Testconstructors () {}
}
The compiler will add code, as follows:
public class Testconstructors {
Testconstructors () {
Super
}
}
Think about it and know the following code
public class Example {}
The following will be added by the compiler code such as:
public class Example {
Example () {
Super
}
}
Inherited
Constructors cannot be inherited. Subclasses can inherit any method of the superclass. Look at the following code:
public class Example {
public void Sayhi {
System.out.println ("Hi");
}
Example () {}
}
public class Subclass extends Example {
}
Class subclass automatically inherits the Sayhi method in the parent class, but the constructor Example () in the parent class cannot be inherited.
Summarize
Topic Builder Methods
function to build an instance of a class Java function statement
Adornments cannot be bstract, final, native, static, or synchronized can
The return type has no return value, no void has a return value, or void
Naming and class names are the same; usually nouns, uppercase starts usually represent the meaning of a verb, lowercase begins
This points to another constructor in the same class that points to an instance of the current class and cannot be used in a static method
Super invokes the constructor of the parent class, calling an overloaded method in the parent class in the first row
Inheritance constructor cannot be inherited by method
The compiler automatically joins a default constructor automatically joins (if not) does not support
The compiler automatically joins a default constructor called to the superclass automatically joins (if not) does not support
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.