two public class in a file

M

mantu

when we write two public class in a single file its give
error.(compilation error)
so,my question is why?what's the region behind this.
 
R

Roedy Green

when we write two public class in a single file its give
error.(compilation error)
so,my question is why?what's the region behind this.

When Java finds a reference to a public class it can calculate what
the name of the corresponding class file is, and look for it on the
classpath. If it does not find it, it can find the corresponding
source file on the sourcepath. Without the naming convention it would
have to open every file on the sourcepath looking for it.

What puzzles me is why default scope classes can be packed more than
one into a source file. How does javac find THEM?
 
J

jmcgill

Roedy said:
What puzzles me is why default scope classes can be packed more than
one into a source file. How does javac find THEM?

I'm sure their names map to their source file, just like the class name
ends up doing.
 
O

Oliver Wong

Roedy Green said:
When Java finds a reference to a public class it can calculate what
the name of the corresponding class file is, and look for it on the
classpath. If it does not find it, it can find the corresponding
source file on the sourcepath. Without the naming convention it would
have to open every file on the sourcepath looking for it.

What puzzles me is why default scope classes can be packed more than
one into a source file. How does javac find THEM?

You'd only have to search a directory, as opposed to the entire
universe.

- Oliver
 
R

Roedy Green

I'm sure their names map to their source file, just like the class name
ends up doing.

Not if you have more than one class per file. There is only one file
name and several unrelated class names. Same package though.
 
R

Roedy Green

You'd only have to search a directory, as opposed to the entire
universe.

that would also apply to public packages which are limited to one per
source file.
 
J

Jeffrey Schwab

Roedy said:
When Java finds a reference to a public class it can calculate what
the name of the corresponding class file is, and look for it on the
classpath. If it does not find it, it can find the corresponding
source file on the sourcepath. Without the naming convention it would
have to open every file on the sourcepath looking for it.

What puzzles me is why default scope classes can be packed more than
one into a source file. How does javac find THEM?

I can only guess that each file at the package level gets grepped (or
equivalent). Shouldn't be too bad unless there are bunches & bunches of
classes in the package. Is there any run-time penalty to this kind of
linear search?
 
R

Roedy Green

I can only guess that each file at the package level gets grepped (or
equivalent). Shouldn't be too bad unless there are bunches & bunches of
classes in the package. Is there any run-time penalty to this kind of
linear search?

It would have to for each segment of the classpath do a File.list()
with a filter for *.java. The scan each file for class xxxx to build
a HashMap of where to find what. It could avoid doing that if all
classes it needs to find are public or have already been added to the
"to compile" list.
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top