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 Beginning Java and the fly likes Static method 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 » Java » Beginning Java
Bookmark "Static method" Watch "Static method" New topic
Author

Static method

Michael Mendelson
Ranch Hand

Joined: Dec 19, 2000
Posts: 73
I'm a little confused about how to use Static methods. Here's my situation:
I'm creating a class which hold some HashMaps. This is basically a container for an entire data set. There will only be one instance in the app. I would like to create methods in this class to do various operations on the entire dataset.
My intuition is to make the HashMaps static. So far, no problem.
The problem comes when I call a method of one of the objects contained in a HashMap. For example, I would like to make a calculation on all objects in a HashMap. The compiler will not allow me to access non-static methods/variables "from a static context."
Why not? Thanks in advance for your response....
Chris Johns
Greenhorn

Joined: Jan 13, 2001
Posts: 15

Hi,
U have made the objects of the type Hash Map static, However this does not mean that the built in non static methods of the class HashMap can be accessed without the HashMap object i.e for accessing a non static HashMap method, one always requires a HashMap object not a HashMap class variable.
Good Luck !!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static method
 
Similar Threads
Serializing classes with hashmaps
Lists
Can you put an object in as a Hashmap value?
How to Ensure I'm NOT Passing RMI Objects I don't Mean to
Confusion about interfaces, creating your own classes, casting