Java Learning Path: The difference between Java constructors and methods

Source: Internet
Author: User
Tags joins

Summary
To learn Java, you have to understand the constructor. Because constructors can provide many special methods, this is often confusing for beginners. However, there are many important differences between constructors and methods.
Original Robert Nielsen Station: www.javaworld.com

We say that the constructor is a method, just like the Australian platypus is a feeding animal. (by: Foreigners like to play metaphor, I also follow translation). To understand the platypus, you must first understand the difference between it and other 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 have a certification exam, to understand the constructor. Here is a brief introduction, and finally a table for some simple summary.

Differences in function and effect
constructor is to create an instance of a class. This process can also be used when creating an object: Platypus p1 = new Platypus ();

Instead, the method works to execute Java code.

modifier, the return value differs from the named
Constructors and methods in the following three convenient differences: modifiers, return values, names. As with methods, constructors can have any access to adornments: public, protected, private, or no adornments (usually called by the package and friendly). Unlike methods, constructors cannot have the following non-accessible adornments: abstract, final, native, static, or synchronized.

The return type is also very important. 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 two names. The constructor uses the same name as the class, and 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 shows an action.

Usage of "This"
Constructors and methods use the keyword this for a very large difference. The method references this to an instance of the class that is executing the method. A static method cannot use the This keyword because a static method does not belong to an instance of the class, so this does not have anything to point to. The This of the constructor points to the other constructor in the same class, with different argument 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 2 constructors with different parameter lists. The first constructor, which gives the member of the class name assignment, the second constructor, invokes the first constructor, giving the member variable name an initial value of "John/mary Doe".

In the constructor, if you want to use the keyword this, then it must be placed in the first row, and if not, it will result in a compilation error.

The use of "super"
Constructors and methods use the keyword super to point to a superclass, but in a different way. Method uses this keyword to execute methods in the 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 ();
}
}

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 placed on the first line, or the compilation will fail. Look at the following example:

public class Superclassdemo {
Superclassdemo () {}
}

Class Child extends Superclassdemo {
Child () {
Super ();
}
}

In the above-mentioned example, the constructor child () contains super, which is to instantiate the constructor Superclassdemo in the superclass and add it to the child class.

The compiler automatically joins the code
The compiler automatically joins the code to the constructor, which may be confusing to novice Java programmers. When we write a class without a constructor, the compiler automatically adds a constructor with no arguments, for example: public class Example {}
After compiling the following code:

public class Example {
Example () {}
}


In the first row 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 code below

public class Example {}

After it is 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. 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.

Summarize

Theme Constructors Method
Function Create an instance of a class Java feature statements
Modified cannot be used,,, bstract final native static , orsynchronized Yes
return type No return value, no void 有返回值,或者void
Named Is the same as the class name, usually a noun, a capital start Usually represents the meaning of a verb, the lowercase beginning
this Point to another constructor in the same class, in the first row Points 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 row Calling one of the overloaded methods in the parent class
Inherited Constructors cannot be inherited Method can be inherited
The compiler automatically joins a default constructor Auto Join (if not) Not supported
compiler automatically joins a default call to superclass constructor Auto Join (if not)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.