Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

question-- a little confused

Thread view: 
Xiaoshen Li - 14 Dec 2005 10:57 GMT
Dear All,

I am confused with the class name and the file name in JAVA. For
example, I have a file called "Test.java". The content of file is:

public class Money
{
    //definitions of the class//

}

public class Driver   //a driver program to test Money class
{
    public static void main(String[] args)
    {
        Money aMoney = new Money();
        ....
    }
}

Now, when I compile the file "Test.java". Errors came out as following:

Test.java:6: class Money is public, should be declared in a file named
Money.java
public class Money
       ^
Test.java:67: class Driver is public, should be declared in a file named
Driver.java
public class Driver

I know my program will work if I put class Money alone in the file
Money.java, put class Driver alone in Driver.java and just compile
Driver.java. But I hope to find out why what I am doing is not working
and how I can make it work.

Thank you very much for your help.
Viator - 14 Dec 2005 15:15 GMT
There are some rules in J2SDK related to how you can name your files
and what classes you can declare in the file.

1) At most one public class can be declared in a file.
2) The name of the file should exactly match with the class name
(except the extension) case by case.
3) You can declare any number of non public classes in the same file.

JAVA itself does not assume any thing about the file system because it
can run on the system where there is no  concept of files.

Amit :-)
Rhino - 14 Dec 2005 15:59 GMT
> There are some rules in J2SDK related to how you can name your files
> and what classes you can declare in the file.
[quoted text clipped - 6 lines]
> JAVA itself does not assume any thing about the file system because it
> can run on the system where there is no  concept of files.

In other words, you can keep your Driver and Money classes together in the
same source file but you need to make sure that only one of them is defined
as public; then, make sure the name of the source file matches the name of
the class which you defined as public.

Therefore, if you decide to define class Money as public and remove the
public identifier from class Driver, the source file should be renamed
Money.java. If you decide to define class Driver as public and remove the
public identifier from class Money, the source file should be renamed
Driver.java. If you want both classes to be public, you will need to put
each in its own separate source file; naturally, public class Money needs to
be in file called Money.java and public class Driver needs to be in file
called Driver.java.

Rhino
Hal Rosser - 15 Dec 2005 00:53 GMT
> Dear All,
>
[quoted text clipped - 32 lines]
>
> Thank you very much for your help.

In the beginning its usually best to put each class in its own file.
The name of the file should be the same as the class - adding the '.java'
extension.
If you have 4 classes - go ahead and make 4 separate files.
You'll learn about putting multiple classes in the same file after you get
more java under your belt.
Keep it simple in the beginning.
f'rinstance...The file named Money.java would have this class declaration.
public class Money{
  // code for the Money class
}
Chris Smith - 15 Dec 2005 00:59 GMT
> I know my program will work if I put class Money alone in the file
> Money.java, put class Driver alone in Driver.java and just compile
> Driver.java. But I hope to find out why what I am doing is not working
> and how I can make it work.

You should put these classes in separate files.  In the end, it really
doesn't matter if you could convince the compiler to accept the code as
is.  It is univerally accepted Java programming style that a top-level
class should be declared in its own source file.  There is simply no
good reason not to comply with that convention.

If you think you have such a reason, please enlighten us... maybe
someone can help solve your actual problem.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.