permaculture playing cards
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public class..." Watch "public class..." New topic

public class...

Gurumurthy Ramamurthy
Ranch Hand

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

Eg. -- source file name

So, the class name should be My:

public class My


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

Joined: Jul 08, 2003
Posts: 24193

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

Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 273
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, it compiled 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.

I agree. Here's the link:
subject: public class...
It's not a secret anymore!