• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 5.0 static import same members collision from different classes  RSS feed

 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks,

Here i have a doubt regarding java 5.0 static imports feature.

Suppose i have two classes Test1 and Test2 which contains static member variable with the same name "NAME" of type String.

If i import it like...
Import com.test.Test1.*;
Import com.test.Test2.*;

Public class TestMain
{
System.out.println(NAME);//here i am expecting the NAME variable from Test1
System.out.println(NAME);//here i am expecting the NAME variable from Test2

//how to avoid name space collision here
//Or let me know what is the better usage of static import in this scenario

}
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way would be to avoid having variables of the same name. It would be kind of a strange design to have variables of the same name in different classes, and have both be so important that they need to be publicly visible.

Having so many static members that an import is worthwhile should be very rare anyway.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case, simply do not use static imports. In fact, limit your static imports as much as possible, and definitely do not use wildcard (*) with static imports. It makes it really hard to figure out where a method comes from.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note you can always use the complete package path to disambiguate -or- avoid the need for import statements entirely.

Bill
 
murali kankanala
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but definetly i can say one thing this is limitaion in Static Imports.

suppose you consider

Import com.xyz.Test1.NAME;
Import com.test.Test2.NAME;

these two packages are from different third Parties where i have a requirement to use those in a same class.
Theose two third parties could not know who and what they are implementing.

Then in my code i must use like

System.out.println(NAME);
System.out.println(Test2.NAME);

If this is the case then i can say this is a limitation with Static Imports.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say that the expectation to be able to use the same identifier -NAME- for two completely different things is a bit unrealistic. I really don't think that qualifies as a "limitation", especially since an easy way to accomplish the same is available, as William notes.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the same as sending a letter to John Smith. Without the address, how do you know which John Smith to send to? In Java, the address is either filled in through an import, through typing in the full path, or a combination (importing the class and typing the class name before the static field).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!