class MyClass
{
public static final int num=90;
}
- Why am I allowed to create a public member in a non-public class?
- Is there another way of accessing this member that I do not know of (other than through the class name)?
class MyClass
{
public static final int num=90;
}
On
Don't forget that classes with default access can be subclassed by public classes in the same package.
package package1;
class MyDefaultClass {
public static final int MY_CONSTANT = 0xCAFEBABE;
}
public class PublicExporter extends MyDefaultClass {
}
Now the public class acts as a bridge, and you are able to consume MyDefaultClass public members from other packages.
package package2;
import package1.PublicExporter;
public class Consumer {
public static void main(String[] args) {
System.out.printf("%x\n", PublicExporter.MY_CONSTANT);
}
}
Consumers can even import static members:
import static package1.PublicExporter.MY_CONSTANT;
public class Consumer {
public static void main(String[] args) {
System.out.printf("%x\n", MY_CONSTANT);
}
}
On
When you declare a variable public it essentially becomes exactly that ; it's able to be seen throughout your entire program, without any special getters/setters. The class does not necessarily need to be public in order for its members to be public also.
Remember, in Java you can only have 1 public class per compilation unit( .java file), and that public class needs to have the same name as the compilation unit. Other than that, it doesn't "own" ownership of the keyword public.
The fact that you declared num as public and static allows you to say System.out.println(MyClass.num). The public attribute allows you to get the num variable directly. Thus, you do not have to create a method to return num for you. Because it is public, you can also say
MyClass mc = new MyClass();
System.out.println(mc.num);
However, since you also added the static declaration, you should only access it via the class name, i.e MyClass.num
Point to take home: public variables can exist in any type of class, and they allow you to access them without the need for getters and setters. Public classes, however, are not the only classes that can own public variables.
On
When a public method belonging to an enclosing class A returns a reference (public supertype reference, like an interface) to its inner class B having default scope, external client (outside A's package) can only call B's methods but can't CREATE themselves fresh instances of B.
If the B's methods weren't public, external client couldn't reach them, and worse: would cause a compilation error since not well implementing its interface.
This modeling could be useful in a certain context, to improve code design.
Since your question was about members, I will address both fields and methods (non-static; Anthony Accioly's answer touches on another good use case, which also includes static fields).
While in many situations this is just an ambiguous consequence of the language's grammar (in particular: public fields in non-public classes, as in your example snippet), there are very good reasons for needing to be able to use public methods in non-public classes.
Expanding on Mik378's answer, consider, e.g., the following (contrived example):
You cannot instantiate one of those
Comparatorimplementations directly outside of that context, but they must override public methods ofComparator, and their functionality is accessible via aComparatorinterface.This same reasoning applies to, e.g.,
privateorprotectedinner classes. If you were not able to declare methodspublic, you would have no way of overridingpublicmethods of interfaces that they inherit or classes that they extends.Practical Examples:
You use this every time you override a public method in an anonymous inner class (e.g. every time you override
public void actionPerformedin an anonymousActionListener).Consider any non-public class that you would like to store in a
HashMap. You would override the publicequals()andhashCode()in that non-public class, and the implementation ofHashMapcan access them regardless of the fact that the class is non-public.The often overridden
public toString()is another common example of a public member of a potentially non-public class.A more complex example is the use of
java.sql.Driverin java.sql.DriverManager (in general, factory-type designs make heavy use of this concept) -- an SQL driver implementation may not make implementation classes public (e.g. the Oracle driver produces non-publicConnectionobjects).Many more... if you keep an eye out for examples of this, you'll be surprised how common it really is!