Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vince Mansel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21344
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need your inner class to be static.
 
Carol Murphy
village idiot
Bartender
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this an example of a doh moment? I want a head-slap emoticon........
 
Vince Mansel
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21344
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vince Mansel wrote:Thanks. My word would be "interesting" because...

Wow... I have learned something..

Hmmm...


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic