• Register
3 votes
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 gives me a compile-time error, you cannot use modifiers on local variables inside a method except final.

I am looking forward to having your explanation.
Thanks.

15.3k points
edited by

Please log in or register to answer this question.

1 Answer

0 votes

Problem:

final is the only variable that cannot be modified after its initial declaration, it has nothing to do with access control.

Have a look below:

public class Test {
    public static void main(String[] args) {
        private int a = 5;
        public int b = 10;
        protected int c = 100;
        static int p = 4;  
        final int i = 50;
    }
}

You’ll get the compile-time error for each of those variables I used under my main function. Hence, the local variable doesn’t require any modifiers. So, the only applicable modifier for a local variable is final.

Let’s see another example:

public class Test { 
    public static void main(String[] args) 
    { 
        final int x; 
        System.out.println("Hello!"); 
    } 
} 

The above program will print Hello! from our program. Here we’ve declared a final in x variable but not initialized. That doesn’t affect our program’s compilation.

I hope everything is clear to you now.
Thanks.

94.3k points

Related questions

0 votes
1 answer 238 views
238 views
Problem: Hello! My question is simple: Which modifiers cannot be used with static variables? Also, I tried running the following program, but I could not understand its output. I am attaching the program and its result below: class test { static test T = new test(); static ... System.out.println(object.num2); } } Output: 10 20 I evaluated the output as 20 40. Can anybody explain the output to me?
asked Dec 19, 2020 Code Learner 10.5k points
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
0 votes
1 answer 2.1k views
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 ... myhigh) myhigh = numbers[myindex]; System.out.println(myhigh); } . . . . } Above block of code is producing the error.
asked Dec 25, 2019 alecxe 7.5k points
1 vote
1 answer 34 views
34 views
Problem: I have a code and it works perfectly fine but when I change the variable as static : private static final List foo;I get compilation error , why ?
asked May 5 Raj Tyagi 16k 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 21 views
21 views
Problem: Where can I get assistance: Cannot assign a value to final variable?
asked Jun 4 Shima 126k 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
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 14 views
14 views
Problem: Is there an efficient way to resolve this > This is a variable whose value is never changed, but it isn't declared with the final key word.
asked Apr 22 ummeshani 10.8k 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