File name different to the class name

Q

qazmlp

GUI@sn65(*)[86]: more different_file_name.java
import java.util.* ;

class println_null
{
public static void main(String arg[] )
{
Vector vec = null ;
System.out.println("I'm printing null" +vec ) ;

}

}
GUI@sn65(*)[87]: javac different_file_name.java
GUI@sn65(*)[88]: java println_null
I'm printing nullnull


As you can see above, the file name and the class name are different and it works.
But, I am getting compilation error, when I change
class println_null
to
public class println_null

Why?
 
A

Andreas Schachl

If your class isnt public the file name is allowed to be different - if your
class is public it has to have the same name as the file!


*hth*
 
S

Stefan Siegl

qazmlp said:
GUI@sn65(*)[86]: more different_file_name.java
import java.util.* ;

class println_null
{
public static void main(String arg[] )
{
Vector vec = null ;
System.out.println("I'm printing null" +vec ) ;

}

}
GUI@sn65(*)[87]: javac different_file_name.java
GUI@sn65(*)[88]: java println_null
I'm printing nullnull


As you can see above, the file name and the class name are different and it works.
But, I am getting compilation error, when I change
class println_null
to
public class println_null

Why?

btw I would suggest using the List interface instead of the Vector class
(if you do not do this already in your other classes)
 
R

rkm

Dario said:
Only public classes must be declared in files with proper names.

- Dario

Not all IDEs enforce this, so if he/she was using an IDE,
then it's possible to get around it. Codewarrior, in
particular, allows you to set a switch to tell it to not
enforce this oddness.

rkm
 
Q

qazmlp

Dario said:
Only public classes must be declared in files with proper names.

Apologies if my question was not clear.

All of you have just repeated what I also observed.But, I wanted to
know why is it so?
I wanted to know why that constraint(file name having the same name as
the class name) exists when I make it as public and not when it has
the default access specifier.
 
D

Dario

Apologies if my question was not clear.

All of you have just repeated what I also observed.But, I wanted to
know why is it so?
I wanted to know why that constraint(file name having the same name as
the class name) exists when I make it as public and not when it has
the default access specifier.

You can have one single file F.java with many (e.g. 3: A, B, C)
classes with default access specifier but (at max) only one public
class (e.g. F).

File F.java:
/////////////////////////////////////////////////////////////////////
class A{public static void main(String[]a){System.out.println("A");}}
class B{public static void main(String[]a){System.out.println("B");}}
class C{public static void main(String[]a){System.out.println("C");}}
public
class F{public static void main(String[]a){System.out.println("F");}}
/////////////////////////////////////////////////////////////////////

and you can invoke them in one of the following 4 ways:

prompt> java A
A
prompt> java B
B
prompt> java C
C
prompt> java F
F

but you cannot add another public class G to file F.java:
/////////////////////////////////////////////////////////////////////
class A{public static void main(String[]a){System.out.println("A");}}
class B{public static void main(String[]a){System.out.println("B");}}
class C{public static void main(String[]a){System.out.println("C");}}
public
class F{public static void main(String[]a){System.out.println("F");}}
public
class G{public static void main(String[]a){System.out.println("G");}}
/////////////////////////////////////////////////////////////////////

Why this?

Because a compiler acting on compilation units residing
on a filesystem may choose to enforce the restriction
that there must be at most one public class per compilation unit.

This optional restriction is allowed by Java Language Specification,
<http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#73491>.

As pointed out by the Java Language Specification:

This restriction makes it easy for a compiler for
the Java programming language or an implementation
of the Java virtual machine to find a named class
within a package; for example, the source code for
a public type wet.sprocket.Toad would be found in
a file Toad.java in the directory wet/sprocket,
and the corresponding object code would be found
in the file Toad.class in the same directory.

This restriction is optional, as pointed out rkm in message
<http://makeashorterlink.com/?C31E555A5>.

This restriction is forbidden by Java Language Specification
when packages are stored in databases (instead of filesystem):
<http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#20150>.

I hope this answer clarify your doubts.

- Dario
 
T

Thomas Weidenfeller

All of you have just repeated what I also observed.But, I wanted to
know why is it so?

Because the language designers decided so. There is nothing more behind
it. They thought it was a good idea to require to have the same name
for a public class and the file. It is an enforcement of a common
coding convention you e.g. find among C++ programmers.

When you decide that the name of a public class has to match the file
name, you have to live with some consequences:

- At most only one public class per file for obvious reasons.

- If you have a public class in the file, it has the file's name. So if
you allow other classes in the file, too (again something which is
allowed in C++), these ones can't have the same name as the file
(that one is taken by the public class). So you must allow these
classes to have different names. It just't can't work otherwise.

And that's all. There is no mystery behind it or some deep technology
breakthrough. It is just a design decission and some logical
consequences of that decission.

/Thomas
 
T

Thomas Weidenfeller

Jacob said:
The original question is still not answered:
Why is it not so for a non-public class?

I answered that: If you enforce it for public classes, you can't
enforce if for non-public classes:

/Thomas
 
Q

qazmlp

Dario said:
Apologies if my question was not clear.

All of you have just repeated what I also observed.But, I wanted to
know why is it so?
I wanted to know why that constraint(file name having the same name as
the class name) exists when I make it as public and not when it has
the default access specifier.

You can have one single file F.java with many (e.g. 3: A, B, C)
classes with default access specifier but (at max) only one public
class (e.g. F).

File F.java:
/////////////////////////////////////////////////////////////////////
class A{public static void main(String[]a){System.out.println("A");}}
class B{public static void main(String[]a){System.out.println("B");}}
class C{public static void main(String[]a){System.out.println("C");}}
public
class F{public static void main(String[]a){System.out.println("F");}}
/////////////////////////////////////////////////////////////////////

and you can invoke them in one of the following 4 ways:

prompt> java A
A
prompt> java B
B
prompt> java C
C
prompt> java F
F

but you cannot add another public class G to file F.java:
/////////////////////////////////////////////////////////////////////
class A{public static void main(String[]a){System.out.println("A");}}
class B{public static void main(String[]a){System.out.println("B");}}
class C{public static void main(String[]a){System.out.println("C");}}
public
class F{public static void main(String[]a){System.out.println("F");}}
public
class G{public static void main(String[]a){System.out.println("G");}}
/////////////////////////////////////////////////////////////////////

Why this?

Because a compiler acting on compilation units residing
on a filesystem may choose to enforce the restriction
that there must be at most one public class per compilation unit.

This optional restriction is allowed by Java Language Specification,
<http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#73491>.

As pointed out by the Java Language Specification:

This restriction makes it easy for a compiler for
the Java programming language or an implementation
of the Java virtual machine to find a named class
within a package; for example, the source code for
a public type wet.sprocket.Toad would be found in
a file Toad.java in the directory wet/sprocket,
and the corresponding object code would be found
in the file Toad.class in the same directory.

This restriction is optional, as pointed out rkm in message
<http://makeashorterlink.com/?C31E555A5>.

This restriction is forbidden by Java Language Specification
when packages are stored in databases (instead of filesystem):
<http://java.sun.com/docs/books/jls/second_edition/html/packages.doc.html#20150>.
I hope this answer clarify your doubts.

Yes, it did.
Thanks a lot for your detailed and clear explanation.
 
R

Roedy Green

Because the language designers decided so. There is nothing more behind
it. They thought it was a good idea to require to have the same name
for a public class and the file.

Naming the file the same as the class has a practical purpose, for
allowing the Java compiler to rapidly find the source for a class if
it can't find the the *.class file.

For example when the compiler is compiling class A and needs to
compile call to a method from class B, it either has to find the class
file for B or the source file for B to know how to do it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top