wood burning stoves 2.0*
The moose likes Cattle Drive and the fly likes Using a static block to init a Hashmap with an inner class object just doesn't 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 » This Site » Cattle Drive
Bookmark "Using a static block to init a Hashmap with an inner class object just doesn Watch "Using a static block to init a Hashmap with an inner class object just doesn New topic
Author

Using a static block to init a Hashmap with an inner class object just doesn't work

Vince Mansel
Greenhorn

Joined: Feb 14, 2010
Posts: 18
In my class, I declare a static Hashmap<K, V> where my V points to an inner class within my class.

Option 1) When I attempt to define a static block to initialize the Hashmap, the compiler
complains with


because of:


That new Apple is my inner class. This does not work.


Option 2) If I keep Apple as an inner class, and I choose to make a non-static-initialization block in my class, and make a new instance of my class
in main, this works!! Yeaaaah!!! Works great.... just not for my nitpicker. I am throwing away a newly created object, never using it. Wrong direction to go.

Option 3) If I keep Apple as an inner class, and put the init code in the constructor for my class, and make a new instance of my class in main, this works.
Yeah!!! .... would not work for my nitpicker. Same reasons as Option 2.

Option 4) If I move the Apple class out of my class, making it an outer class, all is well with the compiler. The static block works great!!!
:wine_and_cheeze: :broken_record: This is not accepatble to my nitpicker. Wants inner classes. :nitpicker_rules:

Does this sound like a familiar Thing? My nitpicker only wants inner classes, and no craziness, which I keep wrapping
my head around Option 1, which still doesn't work?

Any pointers, pushes or fresh approaches are appreciated!
... Need I say more.


paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20488
    ∞

You need your inner class to be static.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1194
Is this an example of a doh moment? I want a head-slap emoticon........
Vince Mansel
Greenhorn

Joined: Feb 14, 2010
Posts: 18
Thanks. My word would be "interesting" becuase...
One thing I forgot to mention:

The inner class Apple is required to inherit from an abstract class Food.
So now I use as an inner class:

If abstract class Food is outside of the my class, the program compiles.
If abstract class Food is inside of my class, the program does not compile.
I have to use static abstract class Food if it is an inner class.

Wow... I have learned something..

Hmmm...
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20488
    ∞

It can work as all inner classes.

The mission to make it work is secondary to making it work right.

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1194
Vince Mansel wrote:Thanks. My word would be "interesting" because...

Wow... I have learned something..

Hmmm...


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using a static block to init a Hashmap with an inner class object just doesn't work
 
Similar Threads
Please Help: Putting Vector objects into HashMap
final variable initialisation(simple code)
confused question
Static
Inner Class