Default constructors in Java

Source: Internet
Author: User

In Java if no constructors are written in a class, there is an argument-free constructor, as follows:

public class Children {private string Name;private string Address;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}

The above code is equivalent to the following:

public class Children {private string Name;private string Address;public Children () {}public string getName () {return name ;} public void SetName (String name) {this.name = name;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}

  

But if you specify a constructor that has a parameter, the parameterless constructor ceases to exist:

So in the spring configuration, the following configuration is wrong:

  <bean id= "Children"  class= "Com.wc82.Children" >  <property name= "Address"  value= "Stresst" > </property>  <property name= "name" value= "Nick" ></property>  </bean>

The above configuration is similar to the following:

Children c=new Children () C.setname ("Nick"); C.setaddress ("Stresst");

However, the default construction method no longer exists, so it will be an error, can be changed to the following configuration:

  <bean id= "Children"  class= "Com.wc82.Children" >        <constructor-arg  value= "1111"  />        <constructor-arg  value= ""  />  </bean>

  

Default constructors in Java

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.