• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why private modifier can't use in top class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic