• Register
1 vote


I am a little curious about java variables. I was trying to figure out how does it work if I don’t initialize a variable after defined. I assumed if the variable is an integer it could be equivalent to 0 and for String, it could be a null value.

I tried to compile my code with this state but it thrown me back Not Initialized. If you attempt to use a local variable before it has been given a value:

Can anybody explain the thing clearly to me? Your answers are appreciated.

12 7 7
15,250 points

1 Answer

1 vote


How exactly a JVM does this is entirely up to the JVM and shouldn't matter for a programmer, since the compiler ensures that you do not read uninitialized local variables. On the other hand, Fields are different. They don’t require to assigned before reading them unless they are Final type. 

The value of a field that has not been assigned is null for reference types or the 0 value of the appropriate primitive type if the field has a primitive type. 

I hope I cleared the things for you. If you have further queries, please reply to this thread.


13 9 6
94,240 points

Related questions

1 vote
1 answer 20 views
Problem: Hello experts, I seek your attention. I am a new student in C++ programming. I have installed all the essential software and about the give my first shot using this programming language. Unfortunately, I’ve encountered an error and that says uninitialized local variable ' ... be a noob question. I know that, but I want to know where am I doing the wrong stuff? Thanks for your help.
asked May 19 adamSw 11.3k points
1 vote
2 answers 56 views
Problem: Hello everyone, I am a newly registered member here. I found you guys so amazing and helpful. Out of curiosity I just started learning the Java programming language and made my very first program that will add two different numbers. public class Main{ public static void main( ... out the error in my code. I know I did something very silly but I am unable to figure out that. Thanks a lot.
asked Apr 6 Gavin 15.3k points
0 votes
1 answer 65 views
Problems:- Can any one help with one of the query I am facing in declaring variable type Declare a reference variable of type File named myFile. Any Idea
asked Feb 13 maddi86 5.4k points
1 vote
1 answer 20 views
Problem: I simply want to know what is the Scope of variables in c++, and why the program throws back errors like, “c++ was not declared in this scope”? Please keep your words as simple as possible. Thanks.
asked Mar 10 Gavin 15.3k points
0 votes
1 answer 24 views
I am a newbee. I was doing my coding practice. I was trying to check if a variable exist in a file or not. I wrote some code to do this : def Check(): if Check.my_variable is None: Check.my_variable = >>> Check.my_variable = None but it gives me error : File "main.py", line 3 Check.my_variable = >>> ^ SyntaxError: invalid syntax What is the error in my code and why am I getting this type of error.
asked Mar 14 LizzyM 6.1k points