• 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

Naming a java file?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is it compulsory that the name of the java file must match the name of the public class ??
 
Saloon Keeper
Posts: 15630
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the designers said so.

Personally I think it's great, because I don't have to look for hours to find the implementation code of something.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naming a java file same as the public class is due to the running phase of the java file or a java class

----The steps to run a java file is
1.first the java compiler is used to compile the java file to provide an output .class file.{COMPILER phase}
2.this output .class file is interpreted by JVM{Interpreter phase}

For example to run file which has class with main method:


[javadoc]public class ABC{
public static void main(String[] args){
System.out.println("hello");
}
}

//other supporting classes
class A{
}[/javadoc]



Example 1:
File aABC.java has a public class ABC
Now during compilation of ABC.java will produce a byte code .class file ABC.class
We need to run this ABC.class file to run our program.

command prompt execution:
>javac ABC.java
>java ABC

Example 2:
File XYZ.java has a public class ABC
Now during compilation of XYZ.java will produce a byte code .class file ABC.class
We need to run this ABC.class file to run our program

command prompt execution:
>javac XYZ.java
>java ABC


---------------------------------------------------------------------------------
This process leads to confusion in case of some large application.
Some times even these small programs also confuse due various process to run the file.

To reduce the confusion to the user Sun decided to directly make a standard that the public class needs to be the file name so that we can refer to the required class easily.
reply
    Bookmark Topic Watch Topic
  • New Topic