[Java] I tried access modifier public and unspecified field access [Eclipse]

Access modifiers used for classes

Access modifier Description
public accessible from anywhere
Unspecified Only accessible from within the same package

Access modifiers used for members (field methods)

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

What if the class is unspecified and the members are private?

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";
}

Anonymous classes cannot be imported!

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)

There is no problem if it is public-public

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.

Conclusion

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.

Banners_and_Alerts_と_Modifier_src_main_Modifier_java_-_Users_onoharamakoto_Desktop-_Eclipse_IDE.png

For the time being I hope it will be helpful for beginners!

Recommended Posts

[Java] I tried access modifier public and unspecified field access [Eclipse]
Access modifier [Java]
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I tried Drools (Java, InputStream)
I tried to make Java Optional and guard clause coexist
I tried using Java REPL
I tried metaprogramming in Java
I tried setting Java beginners to use shortcut keys in eclipse
I tried to translate the error message when executing Eclipse (Java)
I tried to summarize the methods of Java String and StringBuilder
I tried to display the calendar on the Eclipse console using Java.
I compared PHP and Java constructors
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried using Java8 Stream API
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I compared Java and Ruby's FizzBuzz.
I tried using GoogleHttpClient of Java
[Java Silver] Summary of access modifier points
Relationship between kotlin and java access modifiers
I tried Cassandra's Object Mapper for Java
JAVA learning history final modifier and static modifier
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.
I tried using OpenCV with Java + Tomcat
Download and install Eclipse (Java) (Mac version)
I tried Google's entrance exam (unofficial) [java]
I built a Java EE environment on AWS and tried running a web application