Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method

 
Michael Mendelson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic