aspose file tools*
The moose likes Java in General and the fly likes Anonymous Local Inner Class syntax? 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 » Java in General
Bookmark "Anonymous Local Inner Class syntax?" Watch "Anonymous Local Inner Class syntax?" New topic
Author

Anonymous Local Inner Class syntax?

Lance Miller
Greenhorn

Joined: Jun 23, 2008
Posts: 21
I have an local inner class (I think that is the correct terminology) as follows:



Worker is completely self contained and does not implement any interfaces or extend any classes.

Is there syntax that to make that an anonymous class? Basically a way to combine the variable instantiation and the class definition. Something like:

I thought there was syntax do do such. But I can't recall it and could not find anything via googling. It's possible I'm just thinking of a standard anonymous inner class and there is no such syntax. What I have is fine, but it's driving me nuts that I'm sure there is a way to do the latter, but I can't recall. And so to ensure I can sleep tonight, I need to know definitively one way or the other
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

If you want to create an anonymous inner class, it still has to be derived from a known type. Usually that would be an interface, but any base class will do. But if you want to say new Worker, then Worker needs to have already been defined.

You could try and extend Object, but then you'll run into this problem:

But that won't work, because the reference type of worker is Object, so the compiler won't accept the doWork call. You'd have to cast it before calling, but to what?

More generally, regardless of whether you're using an anonymous type or not, when you write worker.doWork(), the reference type of worker must be a type that has the doWork() method. So at the very least you'll need to write an interface to be able to use that approach. If Worker was an interface containing doWork(), then your code would work once a few minor issues have been fixed (doWork() would need to be public and there are a couple of small syntax errors).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Why does that have to be a local class? Why can’t it be a private inner class? Even if it’s a local class, why does it have to be anonymous?
Example of how to write an anonymous class here.
Lance Miller
Greenhorn

Joined: Jun 23, 2008
Posts: 21
Thanks Matthew and Campbell for the responses. I had thought about what you suggested Matthew, but foresaw the problems you mentioned. And, as I mentioned, what I have is fine and works. My question was more of an acedemic one at that point. If I wanted it badly enough, I could have based the annynomus class on a simple inteface like the Guava Function, Guava Predicate, or the Apache Commons Collections Predicate interfaces.

@Campbell: Basically I have a method with the pesudo code:

  • Do some work
  • Do some recursive work that would need to return three values if in another "external" method
  • Do more work

  • The method involes six or so method local variables that all three steps interact with. The middle step recursive logic is fairly simple. That's what I'm using the Local Inner Class for. If I were to move it out to another method or to an inner class, the logic becomes more complex since I need to pass six variables around and create a second data object to have returned. Or add a bunch of global variables just for the sake of one method. As for the second question, it doesn't have to be anonymous. As I mentioned in the original post, what I have works and I was just curious if there was such a thing. I should have put more emphasis in my orignal question that it was an acedemic curiousity based question.

    Again, thank you both for the responses.

    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    It makes no difference to the methods in the local class whether they are simple or complicated. It is probably better, however, to move complicated code into (?private?) methods of the surrounding object and simply call those methods.
    Remember you cannot return more than one value from a Java method. You can alter three fields, you can alter three local variables, or you can wrap them in a single class whose instance you return.

    You probably can’t alter local variables via an anonymous class; local variables used in an anonymous class must be marked final. You can have a final variable which is a reference type which has set and get methods for the three things you want to alter, however.
    Lance Miller
    Greenhorn

    Joined: Jun 23, 2008
    Posts: 21
    Campbell Ritchie wrote:It makes no difference to the methods in the local class whether they are simple or complicated. It is probably better, however, to move complicated code into (?private?) methods of the surrounding object and simply call those methods.

    Exactly. That's why I mentioned it was a simple method. To indicate it did not overly complicate the readability being a Local Inner Class. But it would, IMHO, complicate the readability to move outside the method.

    Campbell Ritchie wrote:Remember you cannot return more than one value from a Java method.

    Umm... yeah... this isn't my first rodeo I mentioned that the method needs to provide three values upon completion, and therefore to indicate that moving the logic to its own method (or a class level inner class) would complicate things since I did not have a single value to return. I would have to create an inner POJO class to encapsulate the values. Or add globals. IMHO though, having gobals, the only purpose of which is to transfer temporary values between two methods is a bit of a code smell. The variables would be null or holding stale irrelevant data except for the few milliseconds this method is called. The use of a Local Inner Class solves these issues and IMHO keeps the code easier to read/grok, not harder.

    Campbell Ritchie wrote:You probably can’t alter local variables via an anonymous class; local variables used in an anonymous class must be marked final. You can have a final variable which is a reference type which has set and get methods for the three things you want to alter, however.

    Correct. I am not changing the variables references. Just values on/in them. Several are collections for example.

    Again, I'll emphasis, this was purely an academic question.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36453
        
      15
    Lance Miller wrote:. . . Or add globals. . . .
    I hope that you don’t actually mean globals. There should be no such thing as a global variable in a Java application. Fields would have been better. But there is nothing wrong with creating a throwaway object which simply exists to encapsulate your three “return” values and pass them to another method.

    In fact you could pass that object as an output parameter, which means its set methods are used to enable it to “carry” the new values to its sending method. I always think you need to be wary when using output parameters, however.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Anonymous Local Inner Class syntax?
     
    Similar Threads
    Question on anonymous class
    Inner Classes when to use them?
    anonymous classes