• Register
0 votes
2.1k views

Problem :

Currently I am having issues while figuring out why am I receiving below error

"Local variables referenced from an inner class must be final or effectively final".

My program is running the concurrent threads to sort the array of #'s and then trying to find the high and the low values of my array. When I created my program without the concurrency I was not facing above error. Now I am struggling to know how to finalize the high and low variable.

public void MyHiLo(int[] numbers){
    int myhigh = numbers[0];
    int mylow = numbers[0];
    Runnable myr2 = new Runnable(){
        @Override
        public void run() {
            System.out.println("My highest value is: ");
            for (int myindex = 1;my index < numbers.length;myindex++){
                if (numbers[myindex] > myhigh)
                    myhigh = numbers[myindex];
                System.out.println(myhigh);
                }
            .
.
.
.
}

Above block of code is producing the error.

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

You are updating both high and low inside your run() method, making them by definition not effectively the final.

As you do not need them outside your run() method anyways so just move your two lines inside as shown below :

public void MyHiLo(int[] numbers){
    Runnable myr2 = new Runnable(){
        @Override
        public void run() {
            int myhigh = numbers[0];
            int mylow = numbers[0];
            System.out.println("My highest value is: ");

 

If you follow above approach your issue will be resolved.

38.6k points

Related questions

0 votes
1 answer 66 views
66 views
Problem: Currently I am having issues and want to figuring out why am I receiving this error:Local variables referenced from an inner class must be final or effectively final.
asked Mar 14 Wafa Abu Yousef 6.1k points
1 vote
1 answer 39 views
39 views
Problem: How to solve this compilation error? Accessed from within inner class needs to be declared final.
asked Apr 25 PkGuy 25.9k points
0 votes
1 answer 52 views
52 views
Problem: Where can I look for a solution to this problem : Local variables referenced from a lambda expression must be final or effectively final?
asked Jun 13 Chi Omega 168k points
3 votes
1 answer 60 views
60 views
Problem: As a beginner, I have a very basic level question. This might be very nonsense to you guys but I need to know. Why is final the only modifier for local variables in Java? I tried to declare final variables with modifiers like all others but it ... -time error, you cannot use modifiers on local variables inside a method except final. I am looking forward to having your explanation. Thanks.
asked Mar 23, 2020 Gavin 15.3k points
0 votes
1 answer 5 views
5 views
Problem: What am I going to do about it : A class declared as final cannot be inherited via the extends keyword?
asked Jun 17 Nahil 128k points
0 votes
1 answer 41 views
41 views
I get the error, as in the subject, and I kindly ask you how to repair it.
asked Dec 30, 2020 TeamScript 13.5k points
0 votes
2 answers 2.2k views
2.2k views
Problem : I face the error, as &ldquo;Local variable defined in an enclosing scope must be final or effectively final&rdquo; and I want to know from you how to repair it the ERROR is in the menu Item-loop, where I am trying to set my textArea foreground colour to the one picked from the menuItem.
asked Jan 4, 2020 alecxe 7.5k points
0 votes
1 answer 78 views
78 views
Problem: Why would one declare an immutable class final in Java?
asked Apr 18 sumaiya simi 43.9k points
0 votes
1 answer 46 views
46 views
Problem: I am trying to count the number of iterations using Java lambda expressions, but it's throwing a compile time error: Local variable count defined in an enclosing scope must be final or effectively final public static void main(String[] args) { List<String> names = new ArrayList<String>(); ... names.add("Steve"); int count = 0; names.stream().forEach(s-> { if(s.length()>6) count++; }); }
asked Apr 26 Moumita Bhowmick 12.9k points
0 votes
1 answer 26 views
26 views
Problem: Has anyone else had a similar experience: Variable used in lambda should be final?
asked Jun 4 Shima 126k points