This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

What will be the name of .java file?

 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is one public class & other public class extends it, means we have two public classes, one is subclass of other.
The main method is defined in subclass.
Then the .java file will be saved by which classname??
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There must be only one public class inside one file. and name of file is must be same as name of public class.
 
birun takhellambam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the subclass name which contain main method will be the .java file name.

thanks.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every class, even inner classes and anonymous classes will get their own file.

If you have an inner class called 'InnerClass' which is defined in OuterClass, there will be two java files : OuterClass.java and OuterClass$InnerClass.java
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an inner class called 'InnerClass' which is defined in OuterClass, there will be two java files : OuterClass.java and OuterClass$InnerClass.java

No. There will be a single source file called "OuterClass.java", which upon compilation will cause two class files to be generated, OuterClass.class and OuterClass$InnerClass.class. But there was no mention of inner classes in the original question, so this is probably besides the point.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
birun takhellambam wrote:the subclass name which contain main method will be the .java file name.

thanks.


But if i am giving the filename as of sub class, i am getting an exception that super class is public, filename should be supreclass.java
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod P Deore wrote:There must be only one public class inside one file. and name of file is must be same as name of public class.


So, for one .java file there will be only one public class & it will also be containing the main method. Right??
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
right
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishnu Sharma wrote:it will also be containing the main method. Right??

Not always. Every public class need not have a main method.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pushkar Choudhary wrote:
Vishnu Sharma wrote:it will also be containing the main method. Right??

Not always. Every public class need not have a main method.


Ok. So in the case, where main method is in different class, which is not public & one public class is also there.
What will be the name of .java file in this case??
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So in the case, where main method is in different class, which is not public & one public class is also there.
What will be the name of .java file in this case??

name of file must be same as name of public class, but when you run that application then you will het runtime exception as
Exception in thread "main" java.lang.NoSuchMethodError: main
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod P Deore wrote:right


Ok. So in the case, where main method is in different class, which is not public & one public class is also there.
What will be the name of .java file in this case??
 
Pushkar Choudhary
Rancher
Posts: 425
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishnu Sharma wrote:in the case, where main method is in different class, which is not public & one public class is also there.
What will be the name of .java file in this case??

The name of the Java file MUST always be the same as the name of the public class.

By the way, if you have 2 classes in one file, on public and another non-public, and if you have the main method in the non-public class, you will not be able to run the main method.
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod P Deore wrote:

So in the case, where main method is in different class, which is not public & one public class is also there.
What will be the name of .java file in this case??

name of file must be same as name of public class, but when you run that application then you will het runtime exception as
Exception in thread "main" java.lang.NoSuchMethodError: main


So, wats the solution for this not to get runtime exception??
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your file contains public class then write main method in that class and save it as the same name as name of public class.
 
Rahul Kurup
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. The class which you tell the compiler to execute MUST have main method. Because main is where execution is supposed to begin. Hence no way you can Workaround that Run time Exception.
2. A java file must have the same name as the only public class(or only default) in it. If you have 2 classes with default access then the file should be named after the class having main method.


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How the source file should be named does not have anything to do with whether the class has a main method or not.

The rule is simple: You can have only one top-level public class per source file. If you put two top-level public classes in the same source file, you will get a compiler error. There is no way around that. The name of the source file must be the same as the name of the single public class that's in the file (with the .java extension, ofcourse).

(By "top-level" I mean a non-nested class).
 
Vishnu Sharma
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:How the source file should be named does not have anything to do with whether the class has a main method or not.

The rule is simple: You can have only one top-level public class per source file. If you put two top-level public classes in the same source file, you will get a compiler error. There is no way around that. The name of the source file must be the same as the name of the single public class that's in the file (with the .java extension, ofcourse).

(By "top-level" I mean a non-nested class).


Ohk. Thanks to all of you guys.
 
Rahul Kurup
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:How the source file should be named does not have anything to do with whether the class has a main method or not.

The rule is simple: You can have only one top-level public class per source file. If you put two top-level public classes in the same source file, you will get a compiler error. There is no way around that. The name of the source file must be the same as the name of the single public class that's in the file (with the .java extension, ofcourse).

(By "top-level" I mean a non-nested class).


Declare two classes - one public and another with default access - in a single source file. Put the main method in the default access class. Now obviously you will name the file with the name of the public class. Compile. No problems. Now try executing it. Please let me know if you are not getting run time exception(NoSuchMethod).

I am a beginner in Java. As per my understanding, the file name must satisfy BOTH the following conditions

1. The name of the file must be that of the public class(which should be only one).This is true if there is a main method in the file or not and the file involves a public class.

2. If the file involves main method then the name of the class that holds the main method must be the name of the file(which should obviously be the name of public class if one is involved). Or in other words, from 1 and 2, you cannot write main method in a default access class if the file involves a public class.

If second one is not followed wherever it is applicable you will get a run time exception.

To be precise, if I have 2 default access classes (only) in a file and i have written main in one of them then the name of the file must be that of this class.


Please correct me in case I am wrong.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul, you are mixing up two different things.

1. A source file must have the same name as the public class in the source file (with the ".java" extension, ofcourse).

2. If your program has a main method, that method must be in a public class, and the method itself must be public static void and take a String[] argument.

You cannot have two classes, one public and one default-acces, in the same file and give the file the name of the default-access class - that will lead to a compiler error because of point number 1. That doesn't have anything to do with whether either class contains a main method or not.

Why mix up those two different things? That only makes it sound more complicated than it really is.
 
Rahul Kurup
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:Rahul, you are mixing up two different things.

2. If your program has a main method, that method must be in a public class, and the method itself must be public static void and take a String[] argument.



Disagree with this. I can have a main method in a default class provided there is no public class in the source file.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, maybe the class doesn't have to be public. But that still hasn't anything to do with the naming of source files.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic