Java parametric construction method and the method of non-parametric construction

Source: Internet
Author: User

Java Construction Method: The method of parameter construction and the method of non-parametric construction


Non-parametric construction method

1. Definition:

If you write a javabean without adding an parameterless construction method, the compiler automatically adds the parameterless constructor method;

However, if you add a parameter constructor without adding an parameterless construct method when you write, the compiler will only recognize the parameter construct method without adding the parameterless constructor by default.

So, if you need to use the parameterless construct method, be sure to add it inside the class


2, for example:

(1) There are 4 JavaBean--------> Noparaminfo.java, Noparaminfo2.java, Noparaminfo3.java, Noparaminfo4.java

public void noparaminfo{

private String name;

Private String adrress;

}

public void noparaminfo2{

private String name;

Private String adrress;

Public Noparaminfo () {}//parameterless construction method

}

public void noparaminfo3{

private String name;

Private String adrress;

A method of parametric construction

Public Noparaminfo (String name,string address) {

This.name=name;

this.address=address;

}

}

public void noparaminfo4{

private String name;

Private String adrress;

Public Noparaminfo () {}//parameterless construction method

A method of parametric construction

Public Noparaminfo (String name,string address) {

This.name=name;

this.address=address;

}

}

(2) test

@Test

public void Testnoparaminfo () {

In this case, the compiler does not report an error

Noparaminfo np=new noparaminfo ();

Write this, the compiler will not complain

NoParamInfo2 np=new NoParamInfo2 ();

In this case, the compiler will report an error, because the parameterless construction method is overridden by a parameter constructor, and the compiler cannot provide a parameterless construction method

NoParamInfo3 np=new NoParamInfo3 ();

In this case, the compiler does not report an error

NoParamInfo4 np=new NoParamInfo4 ();

In this case, the compiler does not report an error

NoParamInfo4 np=new NoParamInfo4 ("Tina", ' Global Village ');

}

two, the method of parametric construction

as the name implies, there are some formal parameters added to the parameterless construction method.





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.