Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What's the difference?

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there! Fellow Programmers !

May I ask a simple Question? [I hope so...]; Here it goes:

What is the difference between a class declared with the public access modifier, and one declared WithOut the public access modifier?

I ask because, in some IDEs when you don't provide the public access modifier, it doesn't allow you to declare the main method. So I suppose there must be a difference, right?

Thank you very much!
 
Chandra Sekar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as i know Main method should always be declared as Public. JVM needs to find and invoke this method. Have you ever tried running your main method without Public keyword ?
[ August 28, 2007: Message edited by: Chandra Sekar ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the original poster is asking about declaring the CLASS public, not just the main method.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!,

yeah, I'm asking about the class being public...

thanks for the replies... Could someone please tell me the difference?
 
John Melton
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, the difference is in how the class is accessed by classes/subclasses in other packages than the package the class is in. For more info, see: http://www.uni-bonn.de/~manfear/javaprotection.php
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Campana:
...in some IDEs when you don't provide the public access modifier, it doesn't allow you to declare the main method...

Hmmm... I don't like that at all. I often write main methods into classes for testing, even if they're not going to be the program's entry point.

In general, a top-level class that's declared public is available everywhere, even outside of its own package. A top-level class with no access modifier has default access, which is restricted to within its own package. (If no package is declared, then it is part of an "unnamed" package, which usually means the current directory.)
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Fellow Ranchers !

Thanks, that claryfied it all!....So a class declared with the public access modifier determines how classes outside the class' package are able to access it...(in this case any class outside can, right)

Yeah, to me it is also disturbing that NetBeans 6.0(Preview) doesn't allow me to declare a if the class doesn't have the public access modifier. It's a pity because I use classes with a main method for testing as well.

Well, thank you very Guys !
Keep up the awesome Work !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic