Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Name Ambiguities

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Name Ambiguities

If a member in one package shares its name with a member in another package and both packages are imported, you must refer to each member by its qualified name. For example, the graphics package defined a class named Rectangle. The java.awt package also contains a Rectangle class. If both graphics and java.awt have been imported, the following is ambiguous.

Rectangle rect;

In such a situation, you have to use the member's fully qualified name to indicate exactly which Rectangle class you want. For example,

graphics.Rectangle rect;


If there is a such circumstance then it doesn't make any sense to import them when ultimately we have to use fully qualified name.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankita modi. wrote:If there is a such circumstance then it doesn't make any sense to import them when ultimately we have to use fully qualified name.

In those circumstances, no, there's, no need to import them. It's relatively rare, though.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65114
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it's java.util.Date and java.sql.Date. Though that pretty much gets resolved by using JPA.
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the old days, java.awt.List and java.util.List also could cause conflicts. They still can if you import java.awt.* and java.util.*.
java.io.FileFilter and javax.swing.filechooser.FileFilter also can cause conflicts, especially since both share approximately the same purpose. I'm still annoyed that javax.swing.filechooser.FileFilter does not implement java.io.FileFilter because it already has its sole method.
 
Ivan Jozsef Balazs
Rancher
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:I'm still annoyed that javax.swing.filechooser.FileFilter does not implement java.io.FileFilter because it already has its sole method.


This reduces the dependency across different packages.
But you can derive a class from it having only the constructors and the "implements" clause.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I didn't ever try to access MAX_VALUE in program then Why there is compile error ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With those import statements, you're telling the compiler "I want to be able to refer to both Integer.MAX_VALUE and Long.MAX_VALUE as simply MAX_VALUE. The compiler's smart enough to realise that doesn't make sense, even without you trying to use them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic