Getting Started with Java: Understanding Constructors (turning)
Source: Internet
Author: User
Getting Started with Java: Understanding the Builder
Translator: Oriental Snake
Understanding the difference between constructors-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");
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service