• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compiling a java code having different file name and class name

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Vijay Arora
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya sure..following is the url where i found the above quote

Compiling a blank file??
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vijay Arora:
Ya sure..following is the url where i found the above quote

Thank you very much.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Samara blue,
Your post was moved to a new topic.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic