Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes What's the difference? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What Watch "What New topic
Author

What's the difference?

Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Oct 10, 2005
Posts: 44
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 ]

SCJP 1.4, SCWCD - Preparing
A pessimist sees difficulty in every oppurtunity, An optimist sees oppurtunity in every difficulty.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

i think the original poster is asking about declaring the CLASS public, not just the main method.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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

Joined: Aug 17, 2004
Posts: 49
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


[url]www.jtmelton.com[/url]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jose Campana
Ranch Hand

Joined: May 28, 2007
Posts: 339
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 !
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What's the difference?
 
Similar Threads
Default access modifier of interface
a top level class in a package?
constructor
Fill in the blank
protected access modifier