• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Trailboss
Posts: 23396
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need your inner class to be static.
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23396
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vince Mansel wrote:Thanks. My word would be "interesting" because...

Wow... I have learned something..

Hmmm...



 
reply
    Bookmark Topic Watch Topic
  • New Topic