• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Source File naming rules

 
Isac paul
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a below code ..

I have given the file name as SourceFileRulesDiffName.java.
It is compiling properly but while running it is throwing class not found exception...
As per the rule
Files with No public classes can have a name that does not match any of the classes in the file
 
Chad Michaels
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may be wrong, but 2 things:

1) A class cannot have members with greater access than the class access modifier itself. For example, if your class has default access, how could it possibly have a public method? Like your main()

2) main() must be public & static, there is no other choice. Which, goes back to point #1.


I'm too sleepy at the moment, but if I had to guess, your problem is main(). Take out the public static void main(), and it should compile fine. Although, you won't be able to run it unless it's within a public class.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chad Michaels wrote:1) A class cannot have members with greater access than the class access modifier itself. For example, if your class has default access, how could it possibly have a public method? Like your main()

No this is not true.

Isac to run the code, you'll have to sue the syntax java <class-name-which-contains-main-method> syntax, so the correct command would be java SourceFileRules...
 
Chad Michaels
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ankit

No this is not true.


Sorry! I have been awake 27 hours. Below, from K&B page 13, is what I was thinking. I see the mistake now. Thanks!

In effect, access means visibility. If class A can't see class B, the access level of the methods and variables within class B won't matter; class A won't have any way to access those methods and variables.


Time for sleep!
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Files with No public classes can have a name that does not match any of the classes in the file


This is true. But for running the class, JVM needs the main() method in the same class as the file name.

Hence the error.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chad Michaels wrote:I have been awake 27 hours.

If I'm awake for more than 20 hours, I have problems recognizing myself in the mirror
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Chad Michaels

Health is Important Dude..... SCJP is not such a difficult exam which need 27 hours of continuous Study!!!

Happy Learning !!!
Cheers!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic