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
posted
0
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 Appleclass 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.
Is this an example of a doh moment? I want a head-slap emoticon........
Vince Mansel
Greenhorn
Joined: Feb 14, 2010
Posts: 18
posted
0
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.