File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why this class compiles ? And Runs correctly ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why this class compiles ? And Runs correctly ?" Watch "Why this class compiles ? And Runs correctly ?" New topic
Author

Why this class compiles ? And Runs correctly ?

Eduardo F. Sandino
Greenhorn

Joined: Dec 10, 2009
Posts: 7
Can anyone explain me - Why this code compiles and runs correctly ?

Test01.java



As far as I know, the class should be public in order to have 1 public class in the file.

Thanks in Advance!
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

Eduardo F. Sandino wrote:As far as I know, the class should be public in order to have 1 public class in the file.


You can have only one public class per file, and as many classes with default access modifier as you want in the same file.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 3133
It compiles because there's no rule that says a class has to be public. There's nothing special about the main() method in that regard either. main() is just another method as far as the compiler is concerned. It's only the JVM that cares.

I thought the rule for the JVM was the both the class and the main() method have to be public, but it appears that's not the case. I wouldn't worry about it. It's not going to affect anything.
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 104
I'm pretty sure that if you don't declare the access modifier, the class is assigned the default access, which is package access. If you only have one file to compile and run that is essentially the same as public access.
Chetan Sarnad
Greenhorn

Joined: Nov 26, 2011
Posts: 20
Jon Swanson wrote:I'm pretty sure that if you don't declare the access modifier, the class is assigned the default access, which is package access. If you only have one file to compile and run that is essentially the same a public access.


No. I am not sure how you concluded that a default access would be similar to public access.
Can you access a single default class in a file outside it's package unlike a public class?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

Jon Swanson wrote:If you only have one file to compile and run that is essentially the same as public access.


Hum... I'm not sure if I agree with this. No matter how many classes you have, package-private classes are always package-private classes. But the thing is, even if your class is package-private and it has the main method, you are able to run it without any problems.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

Chetan Sarnad wrote:Can you access a single default class in a file outside it's package unlike a public class?


No. And JSR294's goal is to allow the creation of modules, and then, even if a class is public, if it is not exported, then it won't also be able to be seen from outside the module.
Chetan Sarnad
Greenhorn

Joined: Nov 26, 2011
Posts: 20
Roberto Perillo wrote:
Chetan Sarnad wrote:Can you access a single default class in a file outside it's package unlike a public class?


No. And JSR294's goal is to allow the creation of modules, and then, even if a class is public, if it is not exported, then it won't also be able to be seen from outside the module.


Yes Roberto. Do agree with you. I was trying to point out the same to Jon Swanson and hence asked him the question i.e Can you access a single default class in a file outside it's package unlike a public class?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2147

Chetan Sarnad wrote:I was trying to point out the same to Jon Swanson and hence asked him the question


Oh! A rhetorical question!
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 104
I was just trying the answer the specific question that was asked. Why the code that was shown would run without declaring the class public. The default access is package-private and if your entire code consists of that one file, you don't have to declare or import a package to run it. And main can access any methods in that one class. So in that case it won't matter that it is package-private. You get the same result compiling and running it as if it were public. I was not talking about a real application that might have multiple classes. Then presumably you would name the package to which the class belonged. Just for the heck of it I created two classes in two separate files without declaring either one public. One had the methods for the application and the other one the main method. Compiled and ran just fine.
 
 
subject: Why this class compiles ? And Runs correctly ?
 
Threads others viewed
Casting of Arrays and Object
Which construtor gets invoked?
casting List to a userdefined object doesn't give compile time error
IDE Warning
toString() and null
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture