Solved

Cannot refer to a non-final variable inside an inner class

Posted on 2004-08-20
5
5,731 Views
Last Modified: 2010-05-18
I have to get an object from an inner class method :

  Object  myObject ;
            
  GetObject(objectKey,new ResultReader () {
    public boolean Read (Collection data) {
      myObject = GetObjectFromCollection(data) ;
    }
  ) ;

  // Now use myObject
  String s = myObject.toString() ;
  // ...

The ResultReader class is an interface, for which the Read method is called by the
'GetObject' method when it passes the object bakc to the caller :

  public interface ResultReader {
    public boolean Read(Collection data);
  }

The problem is that such a code generates an error message at compilation :
"Cannot refer to a non-final variable 'myObject' inside an inner class defined in a different method"
at the line containing "myObject = GetObjectFromCollection(data) ;"


A way to bypass this limitation I have been using is to store the object I get into a Vector defined as 'final',
but I found the solution quite inelegant :

  Object myObject ;
            
  final  Vector v = new Vector() ;

  GetObject(objectKey,new ResultReader () {
    public boolean Read (Collection data) {
      Object myTempObject = GetObjectFromCollection(data) ;
      v.add(myTempObject) ;
    }
  ) ;

  // Get the object back from the Vector
  myObject = v.get(0) ;

  // Now use myObject
  String s = myObject.toString() ;
  // ...

Do you know any other solution that would be more elegant : define the inner class in another manner? declare the object I have to retrieve in some special way?

And why is there such a limitation with the use of inner classes ?

Thanks for answers.

0
Comment
Question by:stsanz
5 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 250 total points
ID: 11851884
this is not a limitation of the inner classes - but limitation of the Anonymous classes - they can access local valiables of the method which are defined as final.
possible solutions -
1. use inner - and not anonymous class
2. use a member variable of the class - instead of mthod local vatiable
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11853323
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 11857540
I don't quite understand your getObject(), is this how it looks in your code? or you're trying to show how it works?

From what is written in getObject(), you are using anonymous class but still an inner class type.

Here is an example to show you that myObject can be assgned within inner class:

public interface Inner{
  public void getObject();
}

class InnerClass{

Object myObject;

void myToString(){
  // Anonymous class with its method call
  (new Inner(){
   public void getObject(){
     myObject = new Object();
   }
  }).getObject();
  System.out.println(myObject.toString());
}

public static void main(String[] args){
  new InnerClass().myToString();  
}


Is this what you're trying to do?
}
0
 

Expert Comment

by:ak_md
ID: 26829780
this is incomprehensible
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countPairs challenge 7 58
mapAB Challlenge 35 85
Image decoding from Camera 3 45
VB Script to add site to Java Exception List 4 13
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now