This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public class...

 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even simpler answer: because it says so in the JLS which is the defining document for the language.
 
Gurumurthy Ramamurthy
Ranch Hand
Posts: 273
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic