Java constructor, this, super

Source: Internet
Author: User

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,

The constructor can have any adornments that are accessed:

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 another constructor in the same class, with different argument lists

, let's look at the following code:

 Public class Platypus {             String name;             Platypus (String input) {                    =
} Platypus () { this ("John/mary Doe"); } Public Static void Main (String args[]) { new platypus ("Digger");
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 cause 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 with this

Keyword to execute methods in the overloaded superclass. Look at the following example:

class Mammal {    
void Getbirthinfo () {
System.out.println ("Born alive." );
}
}classextends 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 () {}  }  }classextends  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

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

After it is added by the compiler code such as:

 Public class Example {  Example () {  super;  }  

Constructors cannot be inherited. Subclasses can inherit any method of the superclass. Take a look at the following code:

 Public class Example {  publicvoid  sayhi {System.out.println ("Hi");} Example () {}}publicclassextends  Example {  }

The class subclass automatically inherits the Sayhi method in the parent class, but the constructor Example () in the parent class cannot be inherited.

Java constructor, this, super

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.