• Register
3 votes
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 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.

12 7 7
15,250 points

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.

13 9 6
94,240 points

Related questions

0 votes
1 answer 52 views
52 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 9.2k points
0 votes
1 answer 16 views
16 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 1.5K views
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 ... myhigh) myhigh = numbers[myindex]; System.out.println(myhigh); } . . . . } Above block of code is producing the error.
asked Dec 25, 2019 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 5 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
1 vote
2 answers 42 views
42 views
Problem: Hello java programmers, Myself is a new student in java. I am practicing basic java methods and objects. Finding beginner-level problems and trying to solve them. I recently got a line that says, all local variables in a method have default values. What does it mean? Can you please write a simple code that makes me understand this well? Thanks guys.
asked Mar 26, 2020 Gavin 15.3k points
0 votes
1 answer 5 views
5 views
Problem: How to set local variable in java, can someone provide me an answer?
asked Apr 5 salim1 34.8k points
1 vote
1 answer 10 views
10 views
Problem: How to fix this erro:the activity must be exported or contain an intent-filter. you cannot use the new command inside an angular cli project
asked Mar 7 MUHAMMAD MUNEEB 82.5k points