HI,
Can someone tell me if two static hashtables declared with the same name in two different classes will cause ambiguity while acessing in each other classes without refering them with class Name.

Hi,

Please find the example,
Consider two classes in the same package,

package p1;

class A
{
static Hashtable ins1 = new Hashtable();
void somemethod()
{
ins1.put("1","1");
}
}

package p1;
class B
{
static Hashtable ins1 = new Hashtable();
void somemethod()
{
ins1.put("1","1");
}
}

no it wouldnt matter, it only matters if you define the same variable twice inside of a method.

Edit: This probably doesn't apply now based upon the example code you posted while I was responding. You mentioned using those two hashtables in other classes without qualifying them by name, which is not what your example indicates.

Of course it will cause ambiguity. The only way to use them without the class name would be to use static imports and if both variables have the same name they cannot be resolved.

Even if you could, why on earth would you?

Also note, unless you have specific reasons for needing Hashtable (if don't know what those reasons are then you don't need it), you should consider using HashMap instead.

and why use public static fields at all?
They're THE worst thing to use in Object Oriented programming, a clear sign that the programmer didn't know the first thing about what she was doing.

hi,
I have just give u the sample only and not the real circumstance where i used it.It does sounds good where i used it.

Thanks,
Vivek

hi,
I have just give u the sample only and not the real circumstance where i used it.It does sounds good where i used it.

Thanks,
Vivek

As jwenting said, public statics for anything but constants is usually a poor idea. There is probably a much better way for you to make that data available to your other classes.

Last company I worked we had a banner on the wall reading "the only good global is an eliminated global".
That's how bad public static variables (and to a somewhat lesser degree methods) are.

This article has been dead for over six months. Start a new discussion instead.