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

3 votes
1 answer 37 views
37 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 12 views
12 views
I get the error, as in the subject, and I kindly ask you how to repair it.
asked Dec 30, 2020 TeamScript 9.2k points
0 votes
2 answers 1.1K views
1.1K 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 15 views
15 views
Why static and final are not compatible together in Java ?
asked Sep 22, 2020 rm02 310 points
0 votes
1 answer 50 views
50 views
Problem : In Java, I want to have something as below : class MyClazz<T> {  static void doIt(T object) {    // More code  } } But I face below error &ldquo;Cannot make a static reference to the non-static type T&rdquo; I am unable to understand ... about a subject. Could someone clarify if such the use is possible, by the similar manner? Also, why was my original attempt went unsuccessful?
asked Jan 21, 2020 jwilliam 3.9k points
0 votes
1 answer 994 views
994 views
Problem : I am getting bellow error can only iterate over an array or an instance of java.lang.iterable
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
1 answer 11 views
11 views
I have a blog website. In this website in several blogs. so from these blog post i need to remove duplicate elements from an array.
asked Sep 28, 2020 Daniel Anderson 4k points
0 votes
1 answer 20 views
1 vote
3 answers 616 views
616 views
Problem : I'm a newbie with Python's arrays and I don't understand how I can avoid below error: python typeerror: list indices must be integers or slices, not str
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 13 views