This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Who can make this code work? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Who can make this code work?" Watch "Who can make this code work?" New topic
Author

Who can make this code work?

Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
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.
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
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!


Anup Engineer
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
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

Joined: May 03, 2002
Posts: 16
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

Joined: Dec 20, 2001
Posts: 3271
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Who can make this code work?
 
Similar Threads
jaxb and complex types extending hashtable , don�t work�.
1:1 Map class?
use hashtables instead of strings
TreeMap Insanity (or is that me?)
getting the int value