• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why this class compiles ? And Runs correctly ?  RSS feed

 
Eduardo F. Sandino
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2273
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!