aspose file tools*
The moose likes Beginning Java and the fly likes public class... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public class..." Watch "public class..." New topic
Author

public class...

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Why the source file name of the public classes should be matching to the class name?

Eg.

My.java -- source file name

So, the class name should be My:

public class My
{

}

I don't understand...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Why? Because if you're compiling a class "A", and it depends on another class B which hasn't been compiled yet, then if it's in a file B.java, the compiler can easily find it. That's all: it's to make it easier to implement a Java compiler.


[Jess in Action][AskingGoodQuestions]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
even simpler answer: because it says so in the JLS which is the defining document for the language.


42
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Hi Ernest Friedman-Hill,

thanks for your answer. But it works even if you don't give public. I have the following classes.

class A
{

}

class B extends A
{

}

as you said, when I compile B.java, it compiled A.java too. Both of the classes are not public. Your answer is exactly correct, if you have not compiled the parent class, having parent class name and source file name same is correct. But, my question was, why should the public classes should have the source name same as the class name.

I also read that it is because requirement of the compilers. But I didn't get, so, shot the question.

Thanks,
Guru
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: public class...
 
Similar Threads
JAVA GUI'S
file name
A very simple question (Please Answer)
Source File declaration rule in java
why a class must be named in the name of public class in a source file?