aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Source File naming rules Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Source File naming rules" Watch "Source File naming rules" New topic
Author

Source File naming rules

Isac paul
Greenhorn

Joined: Dec 06, 2007
Posts: 6
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

Joined: Jun 25, 2010
Posts: 42
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

Joined: Aug 03, 2008
Posts: 9303
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Chad Michaels
Ranch Hand

Joined: Jun 25, 2010
Posts: 42
@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

Joined: Jul 26, 2005
Posts: 189

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.


SCJP 5, SCWCD 5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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

Joined: Sep 12, 2009
Posts: 316
@Chad Michaels

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

Happy Learning !!!
Cheers!!!


SCJP 6.0 96%

(Connecting the Dots ....)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Source File naming rules