?
Solved

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

Posted on 2004-08-20
5
Medium Priority
?
5,763 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Accepted Solution

by:
Venci75 earned 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 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