aspose file tools*
The moose likes Java in General and the fly likes ClassNames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassNames" Watch "ClassNames" New topic
Author

ClassNames

Shamsudeen Akanbi
Ranch Hand

Joined: Dec 24, 2010
Posts: 72
Why is it that sometimes, in the course of running a class, the class name generated differs from the compiled class name.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I don't quite understand the question - but using javac you compile a .java file that can contain many classes within it. The file name should be identical with the public class declared.

So I can save the below code in some file say "Hai.java" and when I compile the java file I get three class files - SomeOther, Outer$Inner and Outer
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3679
    
  16
Shamsudeen Akanbi wrote:Why is it that sometimes, in the course of running a class, the class name generated differs from the compiled class name.

The class name generated by what ? The only time I can think that a class name is generated is when the source code is compiled - class names aren't generated when a Java program is run.
Maybe providing an example of what you mean may make it clearer.


Joanne
Aman Kulkarni
Greenhorn

Joined: Jan 21, 2012
Posts: 15
Hi,
The class name is decided by the compiler on the basis of where the main method [public static void main(String args[])] is declared.

For example:
//File name: Demo.java
class Test
{
public static void main(String args[])
{
System.out.println("Statement in main method");
}
}
class Test1
{
public void check()
{
System.out.println("Statement in diff method");
}

}


Save this file and first compile as javac Demo.java and execute it as java Test

So it is recommended that one must save the file with the name similar to the class containing the main method. One can do the same using a public class.

Hope it will be helpful.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aman Kulkarni wrote:Hi,
The class name is decided by the compiler on the basis of where the main method [public static void main(String args[])] is declared.


No, it's not. It's decided by what you put after the keyword class, or by where you declare an anonymous inner class.

So it is recommended that one must save the file with the name similar to the class containing the main method. One can do the same using a public class.


Wrong, wrong, wrong.
Aman Kulkarni
Greenhorn

Joined: Jan 21, 2012
Posts: 15
Keeping the class name and name of the file same is an unwritten rule...some follow...while some don't...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aman Kulkarni wrote:Keeping the class name and name of the file same is an unwritten rule...some follow...while some don't...


Yes, I know that, but it has nothing to do with the main method.
Aman Kulkarni
Greenhorn

Joined: Jan 21, 2012
Posts: 15
Jeff Verdegan wrote:
Aman Kulkarni wrote:Keeping the class name and name of the file same is an unwritten rule...some follow...while some don't...


Yes, I know that, but it has nothing to do with the main method.


So why don't you tell me the actual parameter of selection of the class name
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

Aman Kulkarni wrote:So why don't you tell me the actual parameter of selection of the class name

Getting snarky with people who are volunteering their time to try and help you is not a strategy for success. I'd strongly advise a reconsideration of your approach.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aman Kulkarni
Greenhorn

Joined: Jan 21, 2012
Posts: 15
Bear Bibeault wrote:
Aman Kulkarni wrote:So why don't you tell me the actual parameter of selection of the class name

Getting snarky with people who are volunteering their time to try and help you is not a strategy for success. I'd strongly advise a reconsideration of your approach.


Ok thanks for your opinion...
Aman Kulkarni
Greenhorn

Joined: Jan 21, 2012
Posts: 15
John Jai wrote:I don't quite understand the question - but using javac you compile a .java file that can contain many classes within it. The file name should be identical with the public class declared.

So I can save the below code in some file say "Hai.java" and when I compile the java file I get three class files - SomeOther, Outer$Inner and Outer


So in the above case...which class name shall I use in order to get the output ???
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aman Kulkarni wrote:
Jeff Verdegan wrote:
Aman Kulkarni wrote:Keeping the class name and name of the file same is an unwritten rule...some follow...while some don't...


Yes, I know that, but it has nothing to do with the main method.


So why don't you tell me the actual parameter of selection of the class name


That question makes no sense. "Parameter of selection of the class name" doesn't mean anything. A class's name is whatever name you give it, right after the class keyword, or a generated name based on the enclosing class name for anonymous inner classes. As I have already indicated.

You made some comments about the main method which didn't make any sense. Any class can have a main method. It's really not clear what point you're trying to make.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aman Kulkarni wrote:
John Jai wrote:I don't quite understand the question - but using javac you compile a .java file that can contain many classes within it. The file name should be identical with the public class declared.

So I can save the below code in some file say "Hai.java" and when I compile the java file I get three class files - SomeOther, Outer$Inner and Outer


So in the above case...which class name shall I use in order to get the output ???


There's no code there that produces any output, so the answer to your question is "none."

When you ask "which class name shall I use", are talking about which class name to provide on the command line when you launch the JVM? If so, then the answer is always, "The class whose main method you want to execute as your program's entry point."
Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47

I don't know whether I should comment or not , whatever no one can stop me

(If you import a class from other package, obviously public class)

Class name and file name should be same only when the class is a public class because compiler searches the source file with the same name as the class name.



Compiler looks at the source files to see if the source is newer. If so, the source file is recompiled.

(If you import a class from the current package, can be non-public class)

But in case of non-public classes there's no need to name the source file as the class name because then compiler searches all the source files of the current package to see which one defines the class.

Please correct me if I am wrong.


Live Curious!!!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sagar Dabas wrote:I don't know whether I should comment or not , whatever no one can stop me

(If you import a class from other package, obviously public class)

Class name and file name should be same only when the class is a public class because compiler searches the source file with the same name as the class name.


There are two mistakes there:

1) Importing has nothing to do with it. The rule applies to the relation between top-level public class names and their class files. It doesn't matter if the class is ever imported or not.

2) According to the spec, the compiler doesn't care. It's only at runtime that the spec mandates that a top-level public class has the same name as it's file. Compilers can additionally enforce the rule for .java files if they wish, but that's implementation specific. (Nevertheless, it's still a good idea to have your class name match its source file name, just to make things easier on yourself.)

Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47

Jeff Verdegan wrote:

There are two mistakes there:

1) Importing has nothing to do with it. The rule applies to the relation between top-level public class names and their class files. It doesn't matter if the class is ever imported or not.

2) According to the spec, the compiler doesn't care. It's only at runtime that the spec mandates that a top-level public class has the same name as it's file. Compilers can additionally enforce the rule for .java files if they wish, but that's implementation specific. (Nevertheless, it's still a good idea to have your class name match its source file name, just to make things easier on yourself.)



Everything is flying over my head , what is top-level public class ? I copied these lines from Core Java by Horstmann and Gary Cornell.

It's only at runtime that the spec mandates that a top-level public class has the same name as it's file.

What runtime has to do with source files ? .class file is not enough for JVM?

By importing I meant : when you use a class from other package in your code.

You mean this is wrong :
When you compile your file, Compiler will search for the source files (the one which you have used from other package) with the same name as the class , (after searching class file) and if the source has been changed compiler will recompile that source file (also when .class file is not found).

And when you use a class from the same package,(class with non-public access ) then it is not necessary the source file and the class file have the same name because in this case compiler does not care what's name your class file , it'll search all the source files in the same package to see which one defines the class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sagar Dabas wrote:
Jeff Verdegan wrote:

There are two mistakes there:

1) Importing has nothing to do with it. The rule applies to the relation between top-level public class names and their class files. It doesn't matter if the class is ever imported or not.

2) According to the spec, the compiler doesn't care. It's only at runtime that the spec mandates that a top-level public class has the same name as it's file. Compilers can additionally enforce the rule for .java files if they wish, but that's implementation specific. (Nevertheless, it's still a good idea to have your class name match its source file name, just to make things easier on yourself.)



Everything is flying over my head , what is top-level public class ?


You know what public means. Top-level means not nested.


I copied these lines from Core Java by Horstmann and Gary Cornell.


Then that book is wrong.

It's only at runtime that the spec mandates that a top-level public class has the same name as it's file.

What runtime has to do with source files ? .class file is not enough for JVM?

Runtime has nothing to do with source files. I'm talking about the .class file. That is, a top-level public class named "Foo" must be in a file named "Foo.class". That is required by the spec. The spec does NOT require that the class comes from Foo.java, although some compiler implementations may decide to impose that restriction.

By importing I meant : when you use a class from other package in your code.


First, importing has nothing to do with using a class from another package. You can use any class without importing it, and you can import a class without using it. Importing is just syntactic sugar to save you some typing and keep your source code less cluttered.

Second, the rule about top-level public classes being in a file with a matching name has nothing to do with importing or with using that class in another package. That rule applies even if the class is never imported and never used in another package.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
Sagar Dabas wrote: . . .
You mean this is wrong :
When you compile your file, Compiler will search for the source files (the one which you have used from other package) with the same name as the class , (after searching class file) and if the source has been changed compiler will recompile that source file (also when .class file is not found).

And when you use a class from the same package,(class with non-public access ) then it is not necessary the source file and the class file have the same name because in this case compiler does not care what's name your class file , it'll search all the source files in the same package to see which one defines the class.
I am not at all sure I understand those two points, but I think one is mistaken.
When you are importing a type, you need to tell the compiler where to find it, which you do with a combination of import declarations and the classpath. [For most beginners the default classpath, which is empty, is sufficient.] The compiler usually expects the file to be in .class format already; it only seems to compile a new file inside the same package.
When you are using a non-public type, the compiler would have to search all the .java files in the source folder until it finds it. I think you are right there.
Aman Kulkarni
Greenhorn

Joined: Jan 21, 2012
Posts: 15
Jeff Verdegan wrote:
Aman Kulkarni wrote:
John Jai wrote:I don't quite understand the question - but using javac you compile a .java file that can contain many classes within it. The file name should be identical with the public class declared.

So I can save the below code in some file say "Hai.java" and when I compile the java file I get three class files - SomeOther, Outer$Inner and Outer


So in the above case...which class name shall I use in order to get the output ???


There's no code there that produces any output, so the answer to your question is "none."

When you ask "which class name shall I use", are talking about which class name to provide on the command line when you launch the JVM? If so, then the answer is always, "The class whose main method you want to execute as your program's entry point."



That's i wanted to know...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aman Kulkarni wrote:
That's i wanted to know...


So is your question answered now? If not, what is "that" which you wanted to know?
Sagar Dabas
Ranch Hand

Joined: Nov 15, 2011
Posts: 47

Campbell Ritchie wrote:
The compiler usually expects the file to be in .class format already; it only seems to compile a new file inside the same package.

Are you saying compiler will not compile files in other package? May be I am not able to understand. Please explain.

But :
These are the lines from Horstmann and Cornell's Core Java :
Compiler first consults all import directives as possible sources for the class. Then compiler goes one step further. It looks at the source files if the source is newer than the class file. If so,the source file is recompiled automatically. Recall that you can import public classes from other packages. A source file can only contain one public class, and the names of the file and public class must match. Therefore, the compiler can easily locate source files for public classes.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39416
    
  28
I think you are going to have to go through the Java™ Virtual Machine Specification. And that means we no longer have a “beginning” subject, so I shall have to move you.
 
 
subject: ClassNames