• 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

Who can make this code work?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to use "a Local Inner Class Example" in Bill Brodgen's "Java 2 Exam Cram" book. The code won't compile "as is", saying "can't make a static reference to a non-static variable...". I could make the two String array variables static, plus make the method makeHash() also static, which compile and run just fine. But what else can I do to make it work without making the variables static? Any other sugguestions on making it more OO will be helpful too.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bad solution:
<code>
import java.util.Hashtable;
public class InnerNotStatic {
static String keys[];
static String names[];

static Hashtable makeHash(String start) {
class myHash extends Hashtable {
public myHash() {
super(10);
}

public void buildHash(String str) {
for (int i=0; i<keys.length; i++) {
if( names[i].startsWith( str )) {
put ( keys[i], names[i]);
}
}
}
}

myHash h = new myHash();
h.buildHash( start );
return h;
}

public static void main(String[] args) {
InnerNotStatic inner = new InnerNotStatic();
keys = new String[] {"k1","k2","k3","k4","k5"};
names = new String[] {"starting", "startup", "nothing", "toy", "boy"};
Hashtable hashtable = new Hashtable(10);
for(int i=0; i<keys.length; i++) {
hashtable.put(keys[i], names[i]);
}
System.out.println(hashtable.toString());
Hashtable hashTemp = new Hashtable(10);
hashTemp = makeHash("start");
System.out.println(hashTemp.toString());
}
}
</code>
I made the variables and method as static.
A good solution would be to provide accessor and mutator methods for keys and names of the InnerNotStatic class. By the way, your InnerNotStatic is not a Inner Class?? or may be I didnt understand your question!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the members keys, names, and makeHash are really supposed to be instance members, then you'll need to reference them with an object as a target. You've already created an object of type InnerNotStatic in main, but you never bother to use it. Make main look like this:

It compiles and runs fine now.
Corey
 
Wei Du
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should point out, the inner class myHash is enclosed in makeHash method. Its purpose is to return a Hashtable object containing a subset of names and keys derived from the original Hashtable. In this case, the new Hashtable only contains key-name pairs where the names start with "start".
Providing accessors is worth a try. Thanks.
 
Wei Du
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did try to use the instantiated object inner to access the variables as Corey showed in the posting. It still complaints at:
hashTemp = makeHash("start");
Corey: did you have to do anything else to make it compile and run besides what you showed in the main ?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice that makeHash is an instance method so, in the main that I showed you, I had to qualify it with a target object. The correct line is, as above:

Corey
 
reply
    Bookmark Topic Watch Topic
  • New Topic