Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > File name different to the class name

Reply
Thread Tools

File name different to the class name

 
 
qazmlp
Guest
Posts: n/a
 
      08-19-2003
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?
 
Reply With Quote
 
 
 
 
Andreas Schachl
Guest
Posts: n/a
 
      08-19-2003
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*


"qazmlp" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
> 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?



 
Reply With Quote
 
 
 
 
Stefan Siegl
Guest
Posts: n/a
 
      08-19-2003
qazmlp wrote:

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

 
Reply With Quote
 
rkm
Guest
Posts: n/a
 
      08-20-2003
Dario wrote:
> qazmlp wrote:
>
>> Why?

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

 
Reply With Quote
 
qazmlp
Guest
Posts: n/a
 
      08-20-2003
Dario <(E-Mail Removed)> wrote in message news:<bhslfg$oh2$(E-Mail Removed)>...
> qazmlp wrote:
>
> > Why?

>
> 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.
 
Reply With Quote
 
Dario
Guest
Posts: n/a
 
      08-20-2003

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


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

 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      08-20-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (qazmlp) writes:
> 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
 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      08-20-2003
Jacob <(E-Mail Removed)> writes:
> 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:

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


/Thomas
 
Reply With Quote
 
Jacob
Guest
Posts: n/a
 
      08-20-2003
Thomas Weidenfeller wrote:
> Jacob <(E-Mail Removed)> writes:
>
>>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:


Sorry. You did. Thanks!



 
Reply With Quote
 
qazmlp
Guest
Posts: n/a
 
      08-20-2003
Dario <(E-Mail Removed)> wrote in message news:<bhv9vr$khd$(E-Mail Removed)>...
> >> 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.

>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
different instances with different data descriptors with the same name Fabrizio Pollastri Python 2 02-18-2008 12:18 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Need Java 2 SDK version 1.4.0, File name is different from file name at the Sun site. SteveSmith@nomail.edu Java 0 03-27-2005 11:05 PM
Any ideas on different file sizes showing of the same file on two different systems? Joe Computer Support 4 01-10-2005 01:05 AM



Advertisments