Study on the default types of Java Constructor

Source: Internet
Author: User

Methods can be modified with the public, private, and protected keywords. The same is true for Constructor methods.

But the constructor can do nothing. In this case, what is the default modifier type?

The following is the study code:

 

Pay attention to the relationship between the packages of these classes.

 

Package com. java. One;/*** the constructor type can be public, private, protected, or not written. * Which of the following is the default type when no data is written? * In this example, to study the default type of constructor * @ author administrator **/public class outerclass {// 1. first, we use this public constructor. r then uses it to declare objects in other classes. Public outerclass () {}// 2. we commented out the above constructor and used the following // outerclass () {}// to find that outerclass two = new outerclass () in the otherpacconstruc class. An error is returned, // it can be inferred that the default constructor without keyword modification is protected! // 3. we will comment out the above two constructor methods, and then we will find that there is no error // so we can draw a conclusion: When the constructor is not written, the compiler creates a public type by default, constructor without any parameters! Public void methodofpublic () {system. Out. println ("public method. ");} Private void methodofprivate () {system. Out. println (" Private method. ");} Protected void methodofprotected () {system. Out. println (" protected method. ");}}

 

Package COM. java. one; public class samepacconstruc {/*** @ Param ARGs */public static void main (string [] ARGs) {outerclass one = new outerclass (); // in the same package, public and protected methods can be called, but private methods cannot be called. One. methodofpublic (); one. methodofprotected ();}}

 

 

Package COM. java. two; import COM. java. one. outerclass; public class otherpacconstruc {/*** @ Param ARGs */public static void main (string [] ARGs) {outerclass two = new outerclass (); Two. methodofpublic (); // not in the same package, only public methods can be called .}}

 

It can be inferred that the default constructor without keyword modification is protected!

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.