Java-4.3 default constructor

Source: Internet
Author: User

Java-4.3 default constructor

In this section, we will discuss some notes about the default constructor and this.

1. default constructor

When you do not set any constructor, the system creates one by default.

 

Package com. ray. ch01; public class Test {}

The code above actually contains a non-argument constructor by default. In this case, you can use new Test () to create an object.

 

However, if you have defined some constructors and do not use the non-argument constructor, you cannot create them using new Test (), because the system defines them by default, no longer added by default.

 

Package com. ray. ch01; public class Test {public Test (int I) {} public static void main (String [] args) {new Test (); // error new Test (1 ); // correct }}

Copy the above error code and run it on your own to know the result.

 

 

Summary: This chapter briefly discusses some notes about the default constructor (no-argument constructor.

 

 

This chapter is here. Thank you.

 

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.