JAVA/JSP Learning Series 9

Source: Internet
Author: User

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


Related Article

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.