aspose file tools*
The moose likes Beginning Java and the fly likes Compiling a java code having different file name and class name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling a java code having different file name and class name" Watch "Compiling a java code having different file name and class name" New topic
Author

Compiling a java code having different file name and class name

Vijay Arora
Greenhorn

Joined: Jun 25, 2007
Posts: 19
Compile the following code having name as Sample1.java :

public class Sample
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}


then I get error as

Sample1.java:1: class Sample is public, should be declared in a file named Sample.java
public class Sample
^
1 error

Now, compile the same code after removing "public" before the class name, this time the code will compile and run without any error.

Compiling a java code, having different file name and class name, works fine if the class is not public but will give compile-time error if the class is public.
Why is it so?
[ April 12, 2008: Message edited by: Viju ]

vijay arora
Shivit Agarwal
Ranch Hand

Joined: Feb 28, 2008
Posts: 82
Even I have the confusion here. The problem is not with the public but else.

Every time i type the command say javac Sample1.java ,instead of my java file Sample.java another java file is created with the name Sample1.java with the content of Sample.java.
Whats the catch behind this ??


Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Remember one thing.
You can have as many classes as you want in your *.java file.But among those classes, only one class can have PUBLIC visibility and that class name should match with your java file name.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi Viju, Welcome to Javaranch
Your name does not follow the Javaranch naming policy please change it.

And yes, the problem is indeed with the public keyword, because according to the compiler you can have only one public class in a Java file, and if you have one then the name of that file must be the same as that of the public class.
Just check the location where your compiled .class files are, you will certainly find a file Sample.class there, no matter what name your source file has.


Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Vijay Arora
Greenhorn

Joined: Jun 25, 2007
Posts: 19
Thanks for the replies..

Yes, i understood that this is the way java works..but my question is WHY?

Why when we make a public class, the JVM is not able to compile it and create a Sample.class file (like it does when no public keyword is used)?

What is the logic behind this?
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Originally posted by Viju:
Now, compile the same code after removing "public" before the class name, this time the code will compile and run without any error.

What do you mean by it ran without any error?
Did it print "Hello World!".
You should check it again.



but my question is WHY?
What is the logic behind this?

'I' think there is no logic behind it.It's simply a rule.
[ April 12, 2008: Message edited by: Vishal Pandya ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Originally posted by Vishal Pandya:

'I' think there is no logic behind it.It's simply a rule.
Please!

There is logic behind it; please do a search through the forum for something like "public file name". I can't remember the exact details, but it has something to do with enabling the compiler to find classes more easily starting from a particular file. I think it is permissible for a compiler to forget this rule, too.
Vijay Arora
Greenhorn

Joined: Jun 25, 2007
Posts: 19
Imagine that you have two files, A.java and B.java containing classes A and B. Furthermore, imagine that class A mentions class B. Now, you type "javac A.java". At some point, the compiler is going to look for B.class, and since it doesn't exist yet, what should the compiler do? Of course, what it does is look for "B.java" and expect class B to be defined in it. If B.java instead contained class C, and C.java contained class B, then the compiler would fail to compile anything.

So even without the rule about one public class per file, you can see another common sense rule: if a class is referenced outside of the source file in which it is defined, the source file should be named after the class. Many Java compilers will warn about violations of this even for non-public classes.

Now, the only reason for a class to be public is for it to be used outside of its own source file, right? So it makes sense to make the rule stronger in this case. The common-sense rule, the one that people follow consciously or not, is that a source file should contain at most one class that is ever mentioned by name outside of the file, and the file should be named after that one class.


Thanks Campbell for the hint..searched through the forum and found the above information.
[ April 14, 2008: Message edited by: Vijay Arora ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Originally posted by Vijay Arora:
Thanks Campbell for the hint..searched through the forum and found the above information.
You're welcome.

Please use the URL tag to quote which thread it came from, so other people can easily read it as well.
Vijay Arora
Greenhorn

Joined: Jun 25, 2007
Posts: 19
Ya sure..following is the url where i found the above quote

Compiling a blank file??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Originally posted by Vijay Arora:
Ya sure..following is the url where i found the above quote
Thank you very much.
Payel Bera
Greenhorn

Joined: Jan 28, 2010
Posts: 10
1) If we have one or more than one non public classes in a java file then what will a legal name of that file?--->Can it be any name apart from the names of the classes in the file or any of the class’ name’s?
2)Why is it that the class sample in file sample1.java , is getting compiled but it is throwing exception at runtime?[java.lang.noclassdeffound error]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Q1: Same as the name of the public class; if no public class it could in theory be anything, if you want to make your .java files hard to find
Q2: That will only happen if there is some other error about the locations of the .class files.
Payel Bera
Greenhorn

Joined: Jan 28, 2010
Posts: 10
Hi Campbell, Thank you for your clarification!!

1) If no public class, then the name of the java file can be the same as any of the non public class or any other name like abc.java,,, please let me know if my understanding is correct.
2) if we have class sample in file sample1.java then the code inside sample.java will execute whenn we run sample1.java,,, please let me know if my understanding is correct. Thanks in advance,.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Payel Bera wrote:Hi Campbell, Thank you for your clarification!!

1) If no public class, then the name of the java file can be the same as any of the non public class or any other name like abc.java,,, please let me know if my understanding is correct.
Have you tried it?

2) if we have class sample in file sample1.java then the code inside sample.java will execute whenn we run sample1.java,,, please let me know if my understanding is correct. Thanks in advance,.
No. You can't run a .java file. You can only interpret .class files.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Samara blue,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compiling a java code having different file name and class name