• Register
0 votes
1.5k 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.

6 5 3
7,540 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.

9 7 4
38,600 points

Related questions

0 votes
1 answer 15 views
15 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
3 votes
1 answer 44 views
44 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 21 views
21 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 1.5K views
1.5K 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 10 views
10 views
Problem: a constant can be defined using using the final keyword. compiler showing this error. Any help will be appreciated. Thanks
asked Feb 25 jyoti goyal 2.1k points
0 votes
1 answer 2 views
2 views
Problem When is it appropriate to use blank final variables? I couldn't find any clear answer to this anywhere.Can someone please help Thanks
asked 4 days ago neeraj 9.5k points
0 votes
1 answer 22 views
22 views
Why static and final are not compatible together in Java ?
asked Sep 22, 2020 rm02 310 points
1 vote
1 answer 2 views
2 views
Problem: Someone asked me about this but I have no any clue. Ref keyword must be used while using out parameter mechanism.
asked Mar 24 PkGuy 13.1k points
0 votes
1 answer 7 views
7 views
Problem: local variables referenced from a lambda expression.How can i clear this problem
asked Mar 21 charles mathews 5.5k points
0 votes
1 answer 2 views
2 views
Problem: I am just having a hard time understanding the concept behind putting wait() in Object class. Any help would be appreciated.
asked Mar 8 ummesalma 22.9k points