Getting Started with Java: Understanding Constructors (turning)

Source: Internet
Author: User
Tags constructor final join joins return string first row access
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");

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:


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 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





Theme

Construction device

Method




Function

Create an instance of a class

Java Feature statement




Modified

Can't use Bstract, final, native, Static,
or synchronized

Yes




return type

No return value, no void

Has a return value, or void




Named

The same as the class name, usually a noun, and a capital opening

Usually represents the meaning of a verb, a lowercase opening




This

Point to another constructor in the same class, in the first line

Point to an instance of the current class and cannot be used for static methods




Super

Invokes the constructor of the parent class, in the first row

Calling an overloaded method in the parent class




Inherited

Constructor cannot be inherited

Method can be inherited




The compiler automatically joins a default constructor

Auto Join (if not)

does not support




The compiler automatically joins a default constructor that calls to the superclass

Auto Join (if not)

does not support

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.