Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert String matching to regex in java 4 50
Bot application - advice 3 38
Eclipse for Java EE development 2 28
junit as external jar or library 7 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

828 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