Java constructor usage Summary

Source: Internet
Author: User

Remember to use the constructor:

 

1. the constructor must have the same name as the class. (If a source file contains multiple classes, the constructor must have the same name as the public class)

2. Each class can have more than one Constructor

3. the constructor can have 0, 1, or more parameters.

4. the constructor does not return values.

5. the constructor is always called along with the new operation.

 

Example:

A. java

 

 

Java code
  1. Public class {
  2. Public (){
  3. System. out. println ("calling constructors without Parameters ");
  4. }
  5. Public A (String mess ){
  6. System. out. println ("constructor with parameters \ n" +
  7. "Parameter content:" + mess );
  8. }
  9. }

Test. java

 

Java code
  1. Public class Test {
  2. Public static void main (String [] args ){
  3. A a_1 = new A (); // call A constructor without Parameters
  4. A a_2 = new A ("Hello"); // call A constructor with Parameters
  5. }
  6. }

Output result:

 

Inheritance and constructor

 

The statement for calling the parent class constructor Using Super must be the first statement of the subclass constructor.

If the subclass constructor does not explicitly call the constructor of the parent class, the default (No parameter) constructor of the parent class is automatically called. If the parent class does not have a constructor without parameters and the constructor of the Child class does not explicitly call the constructor of the parent class, the Java compiler reports an error.

 

Example:

 

A. java

Java code
  1. Public Class {
  2. Public (){
  3. System. Out. println ("calls a's no-argument constructor ");
  4. }
  5. Public A (string mess ){
  6. System. Out. println ("called a constructor with parameters \ n" +
  7. "Parameter content:" + mess );
  8. }
  9. }

 

B. java

Java code
  1. Public Class B extends {
  2. Public B (){
  3. System. out. println ("the no-argument constructor of B is called ");
  4. }
  5. Public B (String mess ){
  6. Super (mess );
  7. System. out. println ("the constructors with parameters of B \ n" +
  8. "Parameter content:" + mess );
  9. }
  10. }

 

Test. java

Java code
  1. Public class Test {
  2. Public static void main (String [] args ){
  3. B B _01 = new B ();
  4. B B _02 = new B ("hello ");
  5. }
  6. }

 

Output result:

 

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.