• 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

Help??

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Java has its strict rules for source file. i.e. there is at most one public class/interface per file and the file name must be identical to this public class/interface.
but what if there is no public class/interface? How to name source file? I have doubt about this because I just came across a review question at Jaworski site : it says that if there is no public class/interface, the name of source file must be differenct than its class/interface. I think here you can name your source file as the class with main() method .
Plz clarify this question for me.
thanks in advance.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right. The filename should be the class which has the main() method. But, what if both the classes have a main() method !!? In that case you would have to decide where you want the entry point of ur pgm to be to have a meaningful execution of the application. Does this make sense ??
Ankur
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. I disagree after testing the following program.
Jason, your assumption that "the name of source file MUST BE differenct than its class/interface" is incorrect.
Actually, it should be "MAY BE". It works fine with either of the class name defined in the file. Just try the following pgm.
Ankur, in case of presence of main() in boht classes, you might want to run each of them separately. you can do so by saying "java Myclass" or "java Yourclass".
// Name it
// Myclass.java or
// YourClass.java or
// Myjava.java
class Yourclass {
public static void main(String[] args){
System.out.println("in main of Yourclass");
}
}
class Myclass{
public static void main(String[] args){
System.out.println("in main of Myclass");
}
}
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranjan,
The example which you have given is not exactly what would happen in normal course. Normally, one would not code, two classes which are not at all related to each other, in one file. Actually u would want to keep ur code as clean as possible by making separate classes as far as possible and instantiating them in other classes as and when needed.
If there are two classes defined in a file and one of them only has main() defined : You can keep the name as any of the two classes and it will compile, but, when u try to run the class file for the class without main() u will obviously get an exception. You would have to decide where you want the entry point of ur pgm to be, to have a meaningful execution of the application.
In ur example :
class Yourclass {
public static void main(String[] args){
Myclass c = new Myclass(); //added 1
System.out.println(c); //added 2
System.out.println("in main of Yourclass");
}
}
class Myclass{
public static void main(String[] args){
System.out.println("in main of Myclass");
}
}
Now, why would u want to name the file as Myclass.java ? When u name this file as Yourclass.java and compile it, Myclass.class gets generated automatically.
I guess, I have actually digressed from the main topic, and what I have written is more to do with how one would do things in an actual scenario rather than from Certification point of view !!
What all you wrote was right but ...
regards,
Ankur
[This message has been edited by Ankur (edited July 10, 2000).]
reply
    Bookmark Topic Watch Topic
  • New Topic