• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 5.0 static import same members collision from different classes

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic