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

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 770 views
770 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 8 views
8 views
Why static and final are not compatible together in Java ?
asked Sep 22 rm02 310 points
1 vote
2 answers 30 views
30 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 Gavin 15.3k points
1 vote
1 answer 50 views
50 views
Problem: I was handling the ArrayList in java and learning through my mistakes. Here I come to know that can I use an ArrayList class method to replace an item at a specific location in an ArrayList? If it is possible, please let me know the way or method which I can use to do this. Thanks
asked Mar 17 Gavin 15.3k points
0 votes
1 answer 340 views
340 views
Problem : I am new to PHP. When I am trying to execute my PHP code then I am facing below error ( ! ) Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) in C:\Users\ mphil\Documents\NetBeansProjects\ ... 45 Call Stack #   Time    Memory  Function    Location 1   0.0000  234208  {main}( )   ..\index.php:0 Please help me in fixing this issue.
asked Feb 15 mphil 2.3k points