• 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

Why this code compiles?

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following Code...
// D.java
class A {}
class C {}
class E {
public static void main(String[] args) {
System.out.println("Strange thing in E.");
}
}
class F {
public static void main(String[] args) {
System.out.println("Strange thing in F.");
}
}
This Code compiles even if there is no public class 'D' defined ! I always assumed that the name of the file must match with the public class name. But I guess i was wrong!!!
If u run it as...
java E
java F
This even runs!!!
And prints the message.
How? JVM fined the main method of the class declared as public and runs it. Then E and F class are not public.
I am really confused!!!
Rashmi
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi,
Strange , but true i too tried out the same thing last night !!
I was surprised that even if my class ( say D.java) does not
have any public classes declared the code compiles and runs.
So i think it is not necessary to have a public class atleast
for running your file !!
Some points which i noticed :
1. As the current example does not have any "public" class
i can save the file by any name. But when i run ( java D ) ,
it gives the runtime error :
Exception in thread "main" java.lang.NoClassDefFoundError: D
2. Suppose if we declare ( say E ) as "public"
then the name of the file you save to should be E.java.
Otherwise you will get compile time error .

3. Also if E is declared as "public" we should not declare
any other class as public
Otherwise you will get compile time error .
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
even i noticed the same points!!! but didn't understand the reason behind it. Somebody pl exaplain...
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rashmi,
I believe Angela already did explain. A file can be saved under any name so long as there is no public class. Whereas, a file with a public class must be saved under that class name.

------------------
Percy Densmore
-SCJP2 Wannabee
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi and Angela,
I believe the behavior you mentioned is correct.
A Java file may contain any number of classes.The file name may or may not match with one of the defined classes.The javac utility only checks for the syntax of the classes defined.For every correct class definition you will see an individual *.class file generated, irrespective of your defining all the class in the same file.
The only criteria for the file to match the name of the class defined is when the class is public.Hence, we cannot have more than one public classes in a file.The public signature of the class tells the JVM to run that particular class.
You will get the following error, when the class defined is public and the file does not have the public class name :
<pre>
class XXXX is public, should be declared in a file named XXXX.java
</pre>

You will normally define a public class in a file, when you have many defined classes and you want to single out which one should be executed using the java command.
If you have a file name and a class name same, but the class is not defined public, it is still syntatically OK, but it will not be possible for the Developer to know which class to run by looking at the file.Hence we define executable class as public; other classes would remain without a public signature.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited August 29, 2001).]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rashmi,
This is indeed an interesting question and I should thank you before telling you what I understood.It has cleared some of my concepts regarding JVM(Java Virtual Machine) and its behavior.Iam learning day by day.The program has no error when it compiles,this is so bcoz the syntaxes are all right.Your assumption that that the name of the file must match with the public class name is true is true.But you should add to your understanding that this assumption is for properly running the java program.What happens is when you call javac at command prompt or in some other tool all the classes in the compilation unit gets compiled creating separate .class files for all the classes in the program.In your caseA.class,C.class,E.class,F.class.Now when you say java E the JVM loads the E.class file fetches the main method (E.main())
.Here it finds main and executes smoothly,same is the case with F.But try calling java D and you will get an error message as
java.lang.NoClassDefFoundError ,since there was no D class in your program.Now try calling java A you will get an error message as java.lang.NoSuchErrorMethod:main, since JVM got no main method in it,as you did'nt define it there.
And as Angela and Sandeep were saying when you define a class as public then it becomes necessary for you to define the name of file same as that of the public class.Else the compiler will give you an error.The only reason I can cite as to why this is so is, when you define a class as public it is now accessible from any package.Now consider this:
import java.io.BufferedReader;
The JVM turns the import statement as
classpath + java/io/BufferedReader.class
loads BufferedReader.class
does the work required.
If Sun had saved the java files BufferRdr.java and class in it as BufferedReader,then scene would be this
import java.io.BufferRdr;
The JVM turns the import statement as
classpath + java/io/BuffrdRdr.class
Could'nt be able to load the required class file,as there does'nt exist one.
Please correct me, if wrong.
THANKS

[This message has been edited by Bindesh Vijayan (edited August 30, 2001).]
[This message has been edited by Bindesh Vijayan (edited August 30, 2001).]
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bindesh,
I appreciate your views.
-- Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bindesh,
Thanx a lot for a wonderful explaination!!! It really cleared my confusion.
Rashmi
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic