Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class and Java file

 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Why should the name of the Java file and the class having the main method be the same?

Waiting for reply. Thanks
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not a requirement, if the class is not marked public.
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I typed the above code in the notepad and named the file as b.java. The compile was successful but when the program was run I got a No Classdef exception. Can anyone explain why this is happening?

Hi Dean you said that only if the class having the main method is marked public,then the file name and the class name should be same. I got that point.

But my question is how do you compile the above "class a" which resides inside the file called "b.java"
[ January 07, 2008: Message edited by: Thirumalai Muthu ]
 
Ganesha Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you compile b.java, the class that is generated is a.class. You have to run the class a. Then you don't get ClassNotFoundException.
 
Thirumalai Muthu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Thanks ganesh I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic