• Post Reply Bookmark Topic Watch Topic
  • New Topic

top level private class  RSS feed

 
Vikash Agarwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a top level class be private?
No. If a class was private.. who could use it? For a normal, top-level class, the only access modifiers that makes sense are public and default(package). Only nested inner classes can be private.

I got a little stumbled. I can ofcourse declare a top level class as private. JAVA does not stop me. Common sense does !!!
But how do we comprehend such questions?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vikash Agarwal:
I can ofcourse declare a top level class as private. JAVA does not stop me.


Wanna bet?
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas,
You cannot declare top level class as private. You will get compiler a error.



Regards
Nik
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i guess you could argue that you CAN indeed write a file that declares a top-level class private. however, would you be allowed to run it, or even compile it? would it be VALID java?

try it and find out!
[ June 05, 2007: Message edited by: Fred Rosenberger ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Fred Rosenberger:
well, i guess you could argue that you CAN indeed write a file that declares a top-level class private.


... only to the extent that you can argue that I can write a time-travelling program by calling java.util.Date.transportMe(). I can type the source code into an editor, but it won't compile, run or do anything.
[ June 06, 2007: Message edited by: Fred Rosenberger ]
 
Vikash Agarwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guys... actually did not know that it would throw compilation error. I perceived it would compile but in non-usable format.
Thanks for clarifying.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Peter Chase:
... only to the extent that you can argue that I can write a time-travelling program by calling java.util.Date.transportMe(). I can type the source code into an editor, but it won't compile, run or do anything

I didn't say it was a JAVA file - I just said it was a file. My goal was to get Vikash Agarwal to try it out, and see what happens.
[ June 06, 2007: Message edited by: Fred Rosenberger ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!