简体   繁体   中英

Using two classes with same name in java

I know this question has been asked a lot on importing two classes and instead omitting the import for both and calling full path whenever you want to use. My question related to this is can we just import one, use that one without the full path and write the full path of the other.

eg

import com.stackoverflow.FirstOne


firstOne ok = new FirstOne();
com.another.folder.firstOne isthisOk = new firstOne();

You have to create a new object with a full package as well:

import com.stackoverflow.FirstOne;

FirstOne ok = new FirstOne(); 
com.another.folder.FirstOne isthisOk = new com.another.folder.FirstOne();

Note: Case is important

Yes...that works.

You can use any number of classes by fully qualified name - and then import one to use by short name.

如果类属于不同的包,则可以,因为对象的静态类型是包的名称+类的名称。

为什么不这样做,因为避免混淆编译器,所以很高兴。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM