Reputation: 463
is there a way to declare 2 classes on the same .java file using Eclipse - also how the compiler will distinguish the .class for each declare class.
example
public class ThisTest
{
public static void main(String[] args)
{
}
}
class SimpleArray
{
}
thank you for your time.
Upvotes: 0
Views: 6933
Reputation: 9608
Yes, exactly like your example.
The extra class need to be non-public
You could also define inner/nested classes. In this case you should investigate the difference
Java inner class and static nested class
public class ThisTest
{
public static void main(String[] args)
{
}
static class SimpleArray
{
}
class SimpleArray2 {}
}
class Buddy {}
Each class will be located in an own .class file in a directory similar to the package. Nested classes get its host prefixed and separated by an '$'. The above case results in four class files
Just check the bin or classes folder of your eclipse project.
Upvotes: 1
Reputation: 420951
is there a way to declare 2 classes on the same .java file using Eclipse
Yes, you can have several classes defined in a single .java file, but there may at most one public class in each file. (Just as in your example.)
Note that if you have a public class in a .java file, the name of that class must be the same as the name of the .java file.
how the compiler will distinguish the .class for each declare class.
The names of the .class files does not depend on the name of the .java file, but on the identifiers of the class declarations.
If you have
class A {}
class B {}
in a file named Classes.java
, you'll get A.class
and B.class
if you compile it.
Upvotes: 3