Solved

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

Posted on 2004-08-20
5
5,734 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
Unable to start eclipse ? 17 129
bitbucket vs gitbucket 3 57
Where to store the queries for modification of table 4 54
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

27 Experts available now in Live!

Get 1:1 Help Now