• Register
3 votes
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 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 11 views
11 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 5.5k points
0 votes
1 answer 1.2K views
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 ... 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 15 views
15 views
Why static and final are not compatible together in Java ?
asked Sep 22, 2020 rm02 310 points
1 vote
2 answers 35 views
35 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 7 views
7 views
Problem: Hi! I am a novice and trying to catch up the concepts in Java. I tried to access a local variable without assigning it a value in main() method and got following error: error: variable might not have been initialized Meanwhile, if I declare the ... variable or the same value is assigned to all variables regardless of datatype? If any of you could explain this, I would be thankful.
asked Nov 29, 2020 Code Learner 5.5k points
1 vote
1 answer 96 views
96 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, 2020 Gavin 15.3k points
0 votes
1 answer 7 views
7 views
Problem: Hello programmers! I am new to the world of Java and I have very little knowledge about the object oriented paradigm with no programming background. I recently learned about “this” keyword as I saw the above mentioned error from Java compiler. I want to know more about ‘this’ keyword. Where can I use it and how to use it?
asked Nov 24, 2020 Code Learner 5.5k points
0 votes
1 answer 464 views
464 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, 2020 mphil 2.3k points
0 votes
1 answer 14 views
14 views
I get the error, as in the subject, and I kindly ask you how to repair it.
asked Dec 30, 2020 TeamScript 11.1k points
0 votes
2 answers 1.2K views
1.2K views
Problem : I face the error, as “Local variable defined in an enclosing scope must be final or effectively final” 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