This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes using new keyword with static inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using new keyword with static inner classes" Watch "using new keyword with static inner classes" New topic

using new keyword with static inner classes

Rich Ramos

Joined: Jun 30, 2010
Posts: 1

I'm trying to figure out how to use table viewers in eclipse jFace. In this question I'm referring to a snippet at the following eclipse page:

In that code there's an outer task with a constructor that instantiates a private static member class but only assigns the result to a reference once. I'm not sure what to make of this. Is the static inner class only instantiated the first time? And does each invocation of "new" only intended to complete the building of a concrete class from the abstract static inner class? I assume the single reference is all that's needed to access that inner object. Do the additional uses of the "new" keyword actually return a reference that can be assigned?

Thanks in advance!
Paul Clapham

Joined: Oct 14, 2005
Posts: 18570

Yes, each of the three "new" expressions represents a different anonymous inner class. Notice that although they each extend the same static inner class, each of them extends it in a different way. Hence, three different anonymous inner classes.

And yes, the static inner class is only loaded (not "instantiated") once. When you load a class, the JVM automatically first loads its superclass if it isn't already loaded. So when you load the first of the anonymous inner classes, by instantiating an object of said class, that causes the static inner class to be loaded.

And yes, "new" always produces a reference to an object which can be assigned. Not just in this particular obscure example, but always.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24187

Hi Rich,

Welcome to JavaRanch!

If you carefully look at the definition of ColumnViewerSorter, you see that in its constructor, it adds an event listener to the viewer you pass as a constructor argument; that listener is connected to the ColumnViewerSorter itself. Therefore just constructing one of these also wires it up, and keeps a reference to it. The only reason the first one is assigned to a variable is so that soon after some other methods can be called on it -- i.e.,

cSorter.setSorter(cSorter, ColumnViewerSorter.ASC);

One last note: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. If "Nj" is not your real last name, you can change your display name here. Thanks!

[Jess in Action][AskingGoodQuestions]
GeeCON Prague 2014
subject: using new keyword with static inner classes