944,129 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2699
  • Java RSS
Oct 23rd, 2007
0

Two Static HashTables

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vivek_green is offline Offline
8 posts
since Aug 2007
Oct 23rd, 2007
0

Re: Two Static HashTables

how would you access them without class names? can you give me an example?
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 23rd, 2007
0

Re: Two Static HashTables

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");
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vivek_green is offline Offline
8 posts
since Aug 2007
Oct 23rd, 2007
0

Re: Two Static HashTables

no it wouldnt matter, it only matters if you define the same variable twice inside of a method.
Reputation Points: 10
Solved Threads: 8
Posting Whiz in Training
nschessnerd is offline Offline
216 posts
since Dec 2006
Oct 23rd, 2007
0

Re: Two Static HashTables

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.
Last edited by Ezzaral; Oct 23rd, 2007 at 1:09 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 23rd, 2007
0

Re: Two Static HashTables

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Oct 23rd, 2007
0

Re: Two Static HashTables

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vivek_green is offline Offline
8 posts
since Aug 2007
Oct 23rd, 2007
0

Re: Two Static HashTables

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 24th, 2007
0

Re: Two Static HashTables

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Regarding JTree
Next Thread in Java Forum Timeline: Please help me to explain these codes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC