Access modifier | Description |
---|---|
public | accessible from anywhere |
Unspecified | Only accessible from within the same package |
Access modifier | Description |
---|---|
public | accessible from anywhere |
protected | accessible within the same package and from subclasses |
Unspecified | Only accessible from within the same package |
private | Only accessible from within the same class |
The created file is as follows.
Class | filename |
---|---|
main class | Modifier.java (main folder) |
public class | Pub.java (test folder) |
Unspecified class | None.java (test folder) |
Modifier.java (list the main method)
package main;
import test.None; //Removed at runtime in Eclipse
import test.Pub;
public class Modifier {
public static void main(String[] args) {
Pub pubclass = new Pub();
None noneclass = new None();
System.out.println(pubclass.pubpub);
System.out.println("Access protected fields of public class");
System.out.println(pubclass.pubpro);
System.out.println("Access unspecified fields in public class");
System.out.println(pubclass.pubnone);
System.out.println("Access the private field of the public class");
System.out.println(pubclass.pubpri);
System.out.println("Access public fields of unspecified class");
System.out.println(noneclass.nonepub);
System.out.println("Access protected fields of unspecified class");
System.out.println(noneclass.nonepro);
System.out.println("Access unspecified fields in unspecified class");
System.out.println(noneclass.nonenone);
System.out.println("Access private fields of unspecified class");
System.out.println(noneclass.nonepri);
}
}
Pub.java
package test;
public class Pub {
public String pubpub = "pubpub";
protected String pubpro = "public proteced";
String pubnone = "public none";
private String pubpri = "pubpri";
}
None.java
package test;
class None {
public String nonepub = "nonepub";
protected String nonepro = "nonepro";
String none = "nonenone";
private String nonepri = "nonepri";
}
As I wrote in the comment above, in Modifier.java
import test.None;
I wrote that, but when I ran it in Eclipse, it was deleted. Console error ~ message
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot resolve None to type
Cannot resolve None to type
Field Pub.pubpro is invisible
Field Pub.pubnone is invisible
Field Pub.pubpri is invisible
at main.Modifier.main(Modifier.java:8)
Pub.java
//Commented out other than public
package test;
public class Pub {
public String pubpub = "pubpub";
// protected String pubpro = "public proteced";
// String pubnone = "public none";
// private String pubpri = "pubpri";
}
None.java
//Added public modifier to class
//Commented out other than public
package test;
public class None {
public String nonepub = "nonepub";
// protected String nonepro = "nonepro";
// String none = "nonenone";
// private String nonepri = "nonepri";
}
Modifier.java
//Commented out all output
//Added to call only the fields of the package's public modifier
package main;
import test.None; //Since it is a public class, it can be imported without being erased.
import test.Pub;
public class Modifier {
public static void main(String[] args) {
Pub pubclass = new Pub();
None noneclass = new None();
System.out.println(pubclass.pubpub);
System.out.println(noneclass.nonepub);
// System.out.println(pubclass.pubpub);
// System.out.println("Access protected fields of public class");
// System.out.println(pubclass.pubpro);
// System.out.println("Access unspecified fields in public class");
// System.out.println(pubclass.pubnone);
// System.out.println("Access the private field of the public class");
// System.out.println(pubclass.pubpri);
// System.out.println("Access public fields of unspecified class");
// System.out.println(noneclass.nonepub);
// System.out.println("Access protected fields of unspecified class");
// System.out.println(noneclass.nonepro);
// System.out.println("Access unspecified fields in unspecified class");
// System.out.println(noneclass.nonenone);
// System.out.println("Access private fields of unspecified class");
// System.out.println(noneclass.nonepri);
}
}
Execution result
pubpub
nonepub
You can now run it without any errors.
I think that will be the case.
Undesignated class protects the iron wall Even in the public class, members other than public are guarding the iron wall
Is that all right?
Since unspecified classes cannot be used outside the package, they can only be used inside the package, so I wonder if they are used for inheritance in abstract classes. The area is still ambiguous, but ...
In the first place, when I wrote it in Eclipse, it notified me of the error with a red wavy line each time. It was worth writing just to realize its convenience.
For the time being I hope it will be helpful for beginners!
Recommended Posts