Solved

non-final variable inside an inner class

Posted on 2002-07-25
3
1,972 Views
Last Modified: 2007-11-27
I get this error:
Cannot refer to non-final variable someParameter
inside an inner class defined in a different method

with this code:

<instantiate a button>
int someParameter = 1;
someButton.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected (SelectionEvent event)
{
  someMethod(someParameter);
}
});

What are the different ways i can get around this?
0
Comment
Question by:jdaues
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7177807
The simplest way is to declare:

final int someParameter = 1;
0
 

Author Comment

by:jdaues
ID: 7177930
well, yes, except that the point of having the parameter is that later in the code I will have

<instantiate button2>
someParameter = 2;
someButton2.addSelectionListener(new SelectionAdapter()
{
public void widgetSelected (SelectionEvent event)
{
 someMethod(someParameter);
}
});
0
 
LVL 4

Accepted Solution

by:
kylar earned 100 total points
ID: 7178050
If it's defined at the class level instead of method level you can pass it as well.

Cheers,
K
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 50
tomcat not starting 6 45
type mismatch (Object[] to double[] 4 23
login form jsp example 2 24
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 …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

813 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

13 Experts available now in Live!

Get 1:1 Help Now