can two packages have same classes in them... having same name....? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

can two packages have same classes in them... having same name....?

20th Aug 2016, 5:43 AM
Amandeep Singh
Amandeep Singh - avatar
8 Respostas
+ 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(); // ...
20th Aug 2016, 8:22 AM
maisterlein
+ 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.
20th Aug 2016, 8:51 AM
maisterlein
0
@yogesh shriram jangid.. . if no.. how can we say packages are used to avoid conflict in names and used to classify classes....
20th Aug 2016, 8:28 AM
Amandeep Singh
Amandeep Singh - avatar
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
20th Aug 2016, 8:33 AM
Amandeep Singh
Amandeep Singh - avatar
0
Ok I got it
20th Aug 2016, 8:34 AM
Yogesh Shriram Jangid
Yogesh Shriram Jangid - avatar
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
20th Aug 2016, 8:56 AM
Amandeep Singh
Amandeep Singh - avatar
- 1
i don't believe so.. it wouldn't know where to point to if you refer to it ..
20th Aug 2016, 5:58 AM
Muerte Exe
Muerte Exe - avatar
- 2
no
20th Aug 2016, 6:33 AM
Yogesh Shriram Jangid
Yogesh Shriram Jangid - avatar