Solved

non-final variable inside an inner class

Posted on 2002-07-25
3
1,978 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
[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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 68
Html Table Looping (part 2) 5 39
jsp CRUD operations with and without prepared statement also hibernatge 1 46
Java List 4 73
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses

739 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