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!