Error. In fact, you can't create two public classes in a single file, Only one class should be public and it should be the name of the class. If you try to create two public classes in same file the compiler generates a compile time error.
Yes there is. You would need to implement your own Classloader and play some games to be able to access both during runtime.
No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public.
If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface. It is generally used when two packages have same class name e.g. java.
You can omit the import statements and refer to them using the entire path. Eg:
java.util.Date javaDate = new java.util.Date()
my.own.Date myDate = new my.own.Date();
But I would say that using two classes with the same name and a similiar function is usually not the best idea unless you can make it really clear which is which.
use the fully qualified name instead of importing the class.
e.g.
//import java.util.Date; //delete this
//import my.own.Date;
class Test{
public static void main(String [] args){
// I want to choose my.own.Date here. How?
my.own.Date myDate = new my.own.Date();
// I want to choose util.Date here. How ?
java.util.Date javaDate = new java.util.Date();
}
}
Yes, when you import classes with the same simple names, you must refer to them by their fully qualified class names. I would leave the import statements in, as it gives other developers a sense of what is in the file when they are working with it.
java.util.Data date1 = new java.util.Date();
my.own.Date date2 = new my.own.Date();
Another way to do it is subclass it:
package my.own;
public class FQNDate extends Date {
}
And then import my.own.FQNDate in packages that have java.util.Date.
If you have your own date class you should distinguish it form the built in Date class. i.e. why did you create your own. Something like ImmutableDate or BetterDate or NanoDate, even MyDate would indicate why you have your own date class. In this case, they will have a unique name.
You can import one of them using import. For all other similar class , you need to specify Fully qualified class names. Otherwise you will get compilation error.
Eg:
import java.util.Date;
class Test{
public static void main(String [] args){
// your own date
my.own.Date myOwndate ;
// util.Date
Date utilDate;
}
}
This scenario is not so common in real-world programming, but not so strange too. It happens sometimes that two classes in different packages have same name and we need both of them.
It is not mandatory that if two classes have same name, then both will contain same functionalities and we should pick only one of them.
If we need both, then there is no harm in using that. And it's not a bad programming idea too.
But we should use fully qualified names of the classes (that have same name) in order to make it clear which class we are referring too.
:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With