aspose file tools*
The moose likes Java in General and the fly likes Access Modifier for Top Level Calsses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Access Modifier for Top Level Calsses" Watch "Access Modifier for Top Level Calsses" New topic
Author

Access Modifier for Top Level Calsses

Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
Hi All,

Just wanted to know
Why can't we use private, and protected modifiers when declaring top-level classes?
I might want my class to be a standalone application with a main method and in such a case why can't I make my class as private.

I have been working on Java for a almost 2 years now and I don't know why did this stuck my mind now

Thanks in advance

- Ankur
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

A "private" class would be one that no other class could access at all, I suppose. This would require that a program launcher have special privileges, I guess; there's no such requirement now.

I guess I don't see this as being especially useful. Do you think it would be?


[Jess in Action][AskingGoodQuestions]
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
I don't think that it will be much useful but yes when we are writing a server etc and we don't want any other class to access the server class we can make it private. Isn't it??

My reason for asking the question was not that I am writing a server and class I wanted to make it private, I just wanted to know why the Java specification does not allow to have package and private access modfiers for the outer classes.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
any other class to access the server class we can make it private. Isn't it??

If a class is top-level, and private, then no other class could access it. Ever. Period. In any way.

It's possible to access a private nested class, because by definition anything private is accessible within the same top-level class. But outside of that top-level class, such a class cannot be accessed. (Unless it conveniently implements an interface (or inherits some other abstract declaration) that is accessible.)

I just wanted to know why the Java specification does not allow to have package and private access modfiers for the outer classes.

Because there's no possible use for a top-level private class, ever. And a top-level protected class would be confusing and unnecessary at best. (I'm omitting discussion of this latter point, for now, because it's more subtle and complex. If we can agree that there's no possible use for a top-level private class, then maybe we will continue with this point.)


"I'm not back." - Bill Harding, Twister
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
Thank you Jim!!

But my point is exactly this:
If a class is top-level, and private, then no other class could access it

My requirement is, I am writing a server class and I don't want it to be accessed by any other class. I have a main method in the server class which will instantiate it and call the start method on it.

Is it that making the outer class as private will cause problems in loading the class itself.

- Ankur
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Originally posted by Ankur Srivastava:
Hi All,

Just wanted to know
Why can't we use private, and protected modifiers when declaring top-level classes?
I might want my class to be a standalone application with a main method and in such a case why can't I make my class as private.

I have been working on Java for a almost 2 years now and I don't know why did this stuck my mind now

Thanks in advance

- Ankur


Use a nested top-level class.

class Container
{
protected class Component
{
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Oops. Forgot static modifier.

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Rick - there seem to be two different definitions of "top-level" being used here. You're using the one that dies at the end of the last milennium. Sun's original "Inner Classes Specification" stipulated that static nested classes are defined as "top-level" for some inane reason. A remarkably poor choice of terminology - how can something nested also be top-level? Fortunately, Sun removed this odious terminology back when they put out the JLS 2nd edition. Nested classes are never "top-level" anymore. And I assume this is the definition Ankur is using, since otherwise the first post in this topic doesn't make sense. Top-level classes can't be private or protected; nested classes aren't relevant to the question.

Ankur - when you start a JVM, you have to give it the name of a top-level class to start with. That class can be public or default access, and it's got to have a public static main(String[]) method. Those are the requirements. You can make any and all other methods private if you like. But it's got to be possible to somehow get into the class from the outside, or there's no possible use for the class.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Jim Yingst:
But it's got to be possible to somehow get into the class from the outside, or there's no possible use for the class.


If you look back at my original reply, you'll see that I postulated that the launcher would have to have the magic ability to load these "private" classes and invoke main() on them, which isn't too much of a stretch. But given that, what he's asking for at least makes sense: an app you can launch, but you can't do anything else to it, not even call main() from another class. It makes sense, but I don't see where it's very useful.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
EFH - yes, I agree that they could give the JVM the ability to do this (provided they also revise the JLS to allow private top-level classes), and I also agree that it doesn't seem very useful. I was responding in terms of what is possible with the JDK as it exists today.
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
Thank you Jim and Ernest!!

As I had mentioned in my mail that there is no specific requirement where I needed a private class but I just wanted to know why the java specification does not allow to have private and protected access to outer classes, the server class which I have talked about in my previous posts just came out of my mind as I was writing the post . I hope it was not very stupid of me.

And Jim can you now please elaborate on this point:
And a top-level protected class would be confusing and unnecessary at best.

- Ankur
Sunil Phani Manne
Greenhorn

Joined: Jun 20, 2005
Posts: 10
Originally posted by Ankur Srivastava:
I don't think that it will be much useful but yes when we are writing a server etc and we don't want any other class to access the server class we can make it private. Isn't it??

Hi Ankur,

Could you be clear on what "Accessing a class from another class" means. Bcoz there are many possibilities:

(1) Creating the instance of a class from another class.
(2) Using the static members of class (they could be methods/variables)
(3) Inheriting the class

Let us talk abt each pt:
(1) If u dont want a class to be instantiated, u can go for a private/protected constructor.
(2) Same is the case with the static members
(3) U could make it final/abstract based on ur need.

Apart from these three scenarios i dont see how we can access a class!

Here one more thing if you want ur server class to contain a main method go ahead and declare main method and make the rest of all the other accesses restrictive using one of the above techniques.

As we have all these facilities to restrict the access to a class, the java developers might not have seen a use of giving private/protected access to the Top level classes!

- Sunil Phani Manne
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
Hi Phani,

Probably the code below will help you to get my point:

I have written a server class

package server;
class Server {

public static void main(String[] args) {
System.out.println("In server main!!");
}
}

Now i jar this, and put the server.jar in classpath of the other class MyServer and the MyServer class is in the same package

package server;
public class MyServer {

public static void main(String[] args) {
System.out.println("In My server main!!");
Server.main(args);
}
}

So you can see I can invoke the main method of Server class and this is something I want to restrict. I hope this will make my point more clear.
Though i know that this is not of much use but still it just came as a thought to me
- Ankur
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
Hi Jim,

Can you please elaborate this point:

"A top-level protected class would be confusing and unnecessary at best."

Thanks
Ankur
 
jQuery in Action, 2nd edition
 
subject: Access Modifier for Top Level Calsses