• Post Reply Bookmark Topic Watch Topic
  • New Topic

Must explicitly qualify the allocation ...  RSS feed

 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the following error...
Must explicitly qualify the allocation with an enclosing instance of type Modifiers (e.g. x.new A() where x is an instance of Modifiers).

...when I try to compile the following:


Any clues?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of an inner class like Moo is associated with a single instance of its enclosing class (Modifiers, here.) When you create an instance of such a class, you have to provide the enclosing instance. That would look like this:

Modifiers mod = new Modifiers();
Moo moo = mod.new Moo();

Often this makes sense; imagine a "Tire" inner class inside a "Car" enclosing class. A Tire belongs to one particular Car.

If this doesn't make sense for your case, then what you want is a static inner class. Such a class isn't tied to an instance of the enclosing class. Just write your inner class like this:



You may be wondering why in useAZoo() you're not required to supply that enclosing instance for the Zoo object. It's because the Moo object has one, and it will be passed along implicitly. But if you make Moo static, and not Zoo, then you'll get a similar compile error in useAZoo(). The fix is the same: make Zoo static, or construct a Modifiers for it.

Finally: if you're investigating inner classes, cool. If you're thinking of writing code like this in general, don't. Inner classes have their uses, but unless you've got a reason to use one, it's better not to.
[ September 16, 2005: Message edited by: Ernest Friedman-Hill ]
 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, which is the best-practice? Adding the static to the inner classes?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need inner classes, but don't need the enclosing instance, then use "static", yes. Otherwise, each inner class instance is carrying around an extra object reference it won't use.

But as I said, unless you need inner classes for some reason, just make all your classes top-level classes.
 
K Robert
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not quite sure I understand... You said need. What exactly does that mean?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Yoshida:
I am not quite sure I understand... You said need. What exactly does that mean?


I think Ernest is suggesting that you think about why you created a inner class in the first place. Does this class need a reference to an instance of the class that currently contains it? If not, then it probably makes sense to create this inner class as a top-level class instead. Your example here looks a little contrived, so it is difficult to explain. For now, I would suggest that you modify it to work both ways so you can understand how the syntax works.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!