Solved

non-final variable inside an inner class

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

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
factorial example 4 47
Android development question 2 53
Java: The Public Class Main 4 30
ejb mdb examples 1 6
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

840 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