0
can two packages have same classes in them... having same name....?
8 Respuestas
+ 2
Yes!
Example:
Package com.myorg.myexample.first contains class MyClass.java
Package com.myorg.myexample.second contains class MyClass.java
In the class where you want to use one of them you just have to import the correct one.
To use the one of the first-package:
import com.myorg.myexample.first.MyClass;
If you want to use both you have to fully qualify them at the time of declaration. eg:
// ...
com.myorg.myexample.first.MyClass first = new MyClass();
com.myorg.myexample.second.MyClass second = new MyClass();
// ...
+ 2
@Aman: yes, i did not only a declaration, i did an initialization at the same time, but thats irrelevant for the example, sorry for confusing you.
if multiple same class names are used in another class you have to write the full qualified name (package name + class name) instead of class name only when declarating your variable, otherwise java can not know which class should be used.
Actually you could always use the full qualified syntax when using a type, then no imports at the top of the class woul be needed, but thats very inconveniant and unreadable, so you just do it when its really necessary.
0
@yogesh shriram jangid.. . if no.. how can we say packages are used to avoid conflict in names and used to classify classes....
0
@maisterlein.......thank you for answering.... i understand your point partially..... what i dont get is "qualify both...at declaration...."......what does ds mean nd u r using d syntax of creating an object.... of class for importing class in package....at d end... pls explain
0
Ok I got it
0
thanks alot...i got it....@maisterlein....what i got is....u r creaing d objects at d same time....when ur importing classes with same names in another class......is ds correct..... nd we cam use ds objects for our purpose..... lastly where ds another class can be placed.. inside any 3rd package
- 1
i don't believe so.. it wouldn't know where to point to if you refer to it ..
- 2
no