954,206 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Two Static HashTables

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.

vivek_green
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

how would you access them without class names? can you give me an example?

nschessnerd
Posting Whiz in Training
216 posts since Dec 2006
Reputation Points: 10
Solved Threads: 8
 

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");
}
}

vivek_green
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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

nschessnerd
Posting Whiz in Training
216 posts since Dec 2006
Reputation Points: 10
Solved Threads: 8
 

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.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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

vivek_green
Newbie Poster
9 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,985 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You