File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why private modifier can't use in top class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why private modifier can Watch "Why private modifier can New topic
Author

Why private modifier can't use in top class

sam lin
Greenhorn

Joined: Mar 05, 2003
Posts: 1
Rules says that private modifier can't use in top class like this:
private class Top{}
public class Test(){
public static void main(String[] args){
}
}
Is any reason why private modifier can't use in top class?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I don�t know the answer to your question, but here is a thought experiment.
Although we cannot declare a top-level class private, we can declare a nested member class as private.
class A { private static class B {} }
If class A has no code except the declaration of B, this is almost the same as declaring A private.
As a final touch, add a main method to A, so that the virtual machine can invoke the main method of B.
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
A private top-level class would never be accessible. You wouldn't be able to extend it, you wouldn't be able to access any of it's members or instatiate it from another class, and you couldn't run it from the command line.
If Java would allow you to compile a top-level private java file, the resulting class would be completely inaccessible.


comp.lang.javascript FAQ: http://jibbering.com/faq/
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Let�s suppose for the moment that the designers of Java had decided to allow a top-level class to be declared as private.
Since the class is private, constructors and methods and fields are not accessible from code in other classes. But code inside the class can access the constructors and members of the class. Code inside the class can create objects of the class and invoke methods of the class. Code inside the class can access other classes.
If the class could be loaded and if the virtual machine could invoke the main method, then the main method can instantiate the class and invoke methods of the class. Those methods can instantiate other classes and invoke methods of those other classes. In other words, we have a class that can do useful stuff for us.
Since static nested classes are like top-level classes and static nested classes can be declared private, a class containing only a private static nested classes is almost private itself.
My na�ve guess would be that the reason top-level classes cannot be declared private is that an object-oriented system is a set of autonomous agents collaborating to perform some higher level function. Private top-level classes are not good at collaborating.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Is any reason why private modifier can't use in top class?
Sometimes I get so involved in the rules and mechanics of Java -- What does mean to access a class? What does it mean to be private? Why are class access and private incompatible? -- that I miss seeing the forest for the trees. Here is a different approach...
A class is an abstraction of a concept. A class encapsulates that abstraction, separating the public interface from the private implementation. The public interface is the class�s contract with the user of the class. The private implementation are the details which can be changed without affecting the contract.
The private members of a class are the implementation details. If a top-level class were private, there would be no public interface. The whole class would be implementation details.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why private modifier can't use in top class