File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes calling a method in another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling a method in another class" Watch "calling a method in another class" New topic

calling a method in another class

Rebecca Witmer
Ranch Hand

Joined: Sep 10, 2004
Posts: 46
ok i am retarded again. back to this running methods from the main program again. i decided that the hash-printing-out function needed to be in its own class because i was using it more than once. so i separated it out like this:

package clustering;

import java.util.*;
import sun.misc.*;
import java.util.regex.*;
import java.util.Hashtable;

public class printHash {
public printHash(Hashtable hash) {
// enumerate all the contents of the hashtable
Enumeration outerKeys = hash.keys();
String outerKey = new String();
String innerKey = new String();
while (outerKeys.hasMoreElements()) {
outerKey = (String) outerKeys.nextElement();
System.out.println("Outer key is: " + outerKey);
Hashtable innerHash = (Hashtable) hash.get(outerKey);
Enumeration innerKeys = innerHash.keys();
while (innerKeys.hasMoreElements()) {
innerKey = innerKeys.nextElement().toString();
System.out.println("\tInner key is: " + innerKey);
System.out.println("\tInner value is: " + innerHash.get(innerKey));
} // end while enumerate



then i put some stuff in the controlling class like this:
printHash p = new printHash(docWordCountHash);

but that is not it. how are you supposed to do it?


SCJP 1.4
marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Rebecca Witmer:
...i decided that the hash-printing-out function needed to be in its own class because i was using it more than once...

Whoa... I'm afraid you're going down an unnecessary road for the wrong reasons. You seem to be defining a new class and creating a new object when all you really need is a method. And it appears you're using this object's constructor as a method -- then "calling" it by creating a new object. :roll:

If you post the code that you initially had trouble with, I think we can find a better alternative.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You are putting your hash printing code in the constructor of the printHash class. [Note: Java convention is to init-cap all class and interface names: PrintHash.] The code at the bottom instantiates a printHash object, passing in the HashTable to be displayed. This runs the code above it.

The following line is illegal because you are not allowed to call a class's constructor directly. It can only be executed when instantiating it with the "new" keyword.

You can leave it as is or put the display code into a separate method of printHash. Or you can turn the constructor into a static method of printHash. I would go for the last option as it seems that the printHash method is a utility method. The printHash class merely provides the utility -- it doesn't have its own state.

To go that route, you'd change your code like this:Also, unless you need to write Java 1.1 code, I'd recommend checking out HashMap and Iterator instead of Hashtable and Enumeration. These are part of the new Java Collections Framework that is much nicer to use in my opinion. All of the classes have standard interfaces (Collection, List, Map, Set) and various implementations (ArrayList, HashMap, TreeSet, etc).
marc weber

Joined: Aug 31, 2004
Posts: 11343

David is correct: HashMap is probably the way to go.

But back to (what appears to be) the issue at hand...

From your code, I'm guessing that you have a Hashtable with values that are themselves Hashtables (thus "outer" and "inner" keys, and explicit casting of inner values to type Hashtable). If that's the case, then does this help?

NOTE: If the method printHash was static, then you wouldn't even need to create a new instance in main. You could just call the method with HashPrinter.printHash(hTableMain);.
[ October 20, 2004: Message edited by: marc weber ]
I agree. Here's the link:
subject: calling a method in another class
It's not a secret anymore!