• Register
1 vote
88 views

Problem:

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.
Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

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.

Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 4 views
4 views
Problem: Hello ... Can anyone tell me that why I got this error? java automatically stores this value in all uninitialized static member variables
asked Apr 6 Ifra 36.4k points
0 votes
1 answer 8 views
8 views
Problem: I am stuck with this ... Please help me that how to deal with this? java automatically stores this value in all uninitialized static member variables:
asked Apr 6 Ifra 36.4k points
1 vote
1 answer 79 views
79 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 12 views
12 views
Problem: Please help me … how can I solve it? what value is stored in uninitialized variables
asked Apr 18 Ifra 36.4k points
0 votes
1 answer 7 views
7 views
Problem: I am stuck here. Tell me what can I do now. "In a pl/sql block a variable is declared as number without an initial value."
asked Apr 10 anika11 32.2k points
1 vote
1 answer 6 views
6 views
Problem: Any ideas on what I need to do to fix this? variable not declared in this scope
asked Mar 13 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 12 views
12 views
Problem: Please help me. How can I solve it? variable was not declared in this scope
asked Mar 13 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 2 views
2 views
Problem: Is it possible to initialize a variable at the time it was declared?
asked Apr 13 nabin shrestha 4.3k points
0 votes
1 answer 2 views
2 views
Problem: I want to ask you if a variable declared inside main method is an instance variable or a local variable. This is the code: public class App { public static void main(String[] args) { Animal fish = new Fish(); ... } } I'm watching a video tutorial from Udemy and the instructor say that fish is an instance variable. I thought that a variable declared inside a method is a local variable.
asked Apr 26 anika11 32.2k points
0 votes
1 answer 6 views
6 views
Problem: Any ideas on what I need to fix this:what is a counter variable in java?
asked Apr 12 ashik 14.6k points