wood burning stoves 2.0*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using new keyword with static inner classes" Watch "using new keyword with static inner classes" New topic
Author

using new keyword with static inner classes

Rich Ramos
Greenhorn

Joined: Jun 30, 2010
Posts: 1
Hi,

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: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/viewers/Snippet040TableViewerSorting.java?view=markup

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
Bartender

Joined: Oct 14, 2005
Posts: 18118
    
    8

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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using new keyword with static inner classes
 
Similar Threads
only 8 days for exam.....
Accessing Outer Class
what can a static local class access ?
inner anonymous class fields initialization
inner classes static reference ambiguity