• 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

Please log in or register to answer this question.

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 50 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, 2020 adamSw 11.3k points
0 votes
1 answer 7 views
Problem: Hello! I just started learning programming in Java with no prior knowledge of any other programming language. Today I attempted a quiz in which there was a question that asked me to declare a string variable and name it as ‘title’. I attempted the ... follows: string title; I have not worked with strings alot. So want to know whether my answer is correct or not? Thanks!
asked 4 days ago Code Learner 6k points
1 vote
2 answers 262 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, 2020 Gavin 15.3k points
0 votes
1 answer 136 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, 2020 maddi86 5.4k points
1 vote
1 answer 47 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, 2020 Gavin 15.3k points
0 votes
1 answer 6 views
Problem: Error occurred during initialization of VM Too small initial heap. this is strange because dtnode_heapsize is 8G DataNode maximum Java I just got this error message trying to change it to 1gig. Error occurred during initialization of VM. Too small initial heap for new size specified. I'm running 64bit
asked 6 days ago Ali 5.1k points
0 votes
1 answer 16 views
Problem: What I want to do is just to define a variable in a header file and use it on two different cpp files without redefinition that variable each time I include that header Here is how I tried : Variables.h #ifndef VARIABLES_H // header guards #define VARIABLES_H static bool bShouldRegister; #endif
asked Dec 15, 2020 Mashhoodch 9.6k points
0 votes
1 answer 30 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, 2020 LizzyM 6.1k points
0 votes
1 answer 16 views
Problem: Why Iso c++ forbids variable length array?
asked Feb 14 jasimchaudary 640 points