aspose file tools*
The moose likes Beginning Java and the fly likes What will be the name of .java file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What will be the name of .java file?" Watch "What will be the name of .java file?" New topic
Author

What will be the name of .java file?

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
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

Joined: Jul 15, 2008
Posts: 629

There must be only one public class inside one file. and name of file is must be same as name of public class.


Life is easy because we write the source code.....
birun takhellambam
Greenhorn

Joined: Mar 06, 2010
Posts: 4
the subclass name which contain main method will be the .java file name.

thanks.


birun
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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


JDBCSupport - An easy to use, light-weight JDBC framework -
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
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.


Ping & DNS - my free Android networking tools app
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
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

Joined: Feb 03, 2010
Posts: 55
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

Joined: Jul 15, 2008
Posts: 629

right
Pushkar Choudhary
Rancher

Joined: May 21, 2006
Posts: 425

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

Joined: Feb 03, 2010
Posts: 55
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

Joined: Jul 15, 2008
Posts: 629


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

Joined: Feb 03, 2010
Posts: 55
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

Joined: May 21, 2006
Posts: 425

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

Joined: Feb 03, 2010
Posts: 55
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

Joined: Jul 15, 2008
Posts: 629

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

Joined: Dec 11, 2009
Posts: 9
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.



Rahul J.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14274
    
  21

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).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
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

Joined: Dec 11, 2009
Posts: 9
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

Joined: Aug 16, 2005
Posts: 14274
    
  21

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

Joined: Dec 11, 2009
Posts: 9
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

Joined: Aug 16, 2005
Posts: 14274
    
  21

Ok, maybe the class doesn't have to be public. But that still hasn't anything to do with the naming of source files.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What will be the name of .java file?