question-- a little confused

X

Xiaoshen Li

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

Viator

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

Rhino

Viator said:
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.
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
 
H

Hal Rosser

Xiaoshen Li said:
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.
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
}
 
C

Chris Smith

Xiaoshen Li said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top