• 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 global variable issue

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am currently having a problem with this code. See these global variables, they are updated fine in the main method. However in the createHash() method, I test the array but issuing a print statement and get a load of 0's. (hash is originally set to = 0 in main)

Can someone offer a solution to fix this issue?

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Sumner wrote:Can someone offer a solution to fix this issue?


Best one I can think of is to get rid of them; or at the very least make them either private or final or both. Not only are global variables bad, but a couple of yours are arrays, which means that anyone can also update the contents at any time, which sounds nightmarish to me.

Winston
 
Sean Sumner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is, I need to use these in this other method to create a hash table.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But they should still be private. The only variables which ought not to be private are constants, and even those ought to be primitives or immutable reference types, so their state cannot change. As Winston has already implied, arrays are implicitly mutable.

If they are fields of your class, then your class should use them, and work out hash codes, etc.
Are you using the java.util.Hashtable legacy class (don't: use HashMap instead) or have you written your own? You do realise that if you use a hash code on a “K” in a hash collection and that hash code changes, you may never find the “K‑V” pair again?
Why are those fields all marked static? Fields default to being non‑static (=instance fields), unless you have a good reason for their being static. And a compiler error is a very bad reason for making anything static.
Why are you not closing those Readers? I think you ought to handle any Exceptions, not declare them.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic