wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Clarification needed on Local variable initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Clarification needed on Local variable initialization" Watch "Clarification needed on Local variable initialization" New topic
Author

Clarification needed on Local variable initialization

Shiveen Pandita
Greenhorn

Joined: Jul 30, 2012
Posts: 25

Hey guys

I'm just beginning in java.

I just stumbled on this code snippet.... how come does it compile when the local variable 'price' hasn't been initialized. please help me

here's the code
-------------------



[Added code tags - see UseCodeTags for details]
Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
Hi. There are two types of variables, global and local. Global (or class/static variables) are declared outside any methods, and they do have default values. Like for boolean false, object references null, and zero for numeric values. Local variables (declared inside methods) don't have default values, so you have to assign that when declaring. Your program may compile fine, because compiler throws you a warning not an error.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
For the following code snippet,

Compiler will eliminate the redundant code(if(true)... which is always true.) as follow:


You can try out with following; in which you will get an error:



Because during compilation, Compiler don't know what is k?? It will be evident at Runtime only...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38461
    
  23
Gaurangkumar Khalasi wrote:For the following code snippet, . . .
Compiler will eliminate the redundant code(if(true)... which is always true.) . . .
Have you tried it? There is a simple way to try it: put that inside a class, compile it, and view the bytecode with javap -c Foo
That will demonstrate whether the if (true) is in fact optimised away. I have tried it myself, so I know the answer.
You can try out with following; in which you will get an error:
. . .
Because during compilation, Compiler don't know what is k?? It will be evident at Runtime only...
You can only convince the compiler that it knows the value of k if k is final.
But when I tried it on Eclipseâ„¢, it compiled and ran all right. It only caused problems if I tried to use the price field afterwards. That is when the error occurs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38461
    
  23
Okti Wm wrote:Hi. There are two types of variables, global and local. . . .
No, fields and locals. The global variable in C does not occur in Java, but you can use public static final fields as constants, which are similar to global constants. You can mimic global variables, but they are error‑prone and should be avoided.
Local variables (declared inside methods) don't have default values, so you have to assign that when declaring. Your program may compile fine, because compiler throws you a warning not an error.
You have to assign definitely to a local variable before using it. The compiler raises an error, not a warning, for failure to assign a local variable definitely.
Waldemar Macijewski
Ranch Hand

Joined: Jun 22, 2012
Posts: 32
Campbell Ritchie wrote:No, fields and locals. The global variable in C does not occur in Java, but you can use public static final fields as constants, which are similar to global constants. You can mimic global variables, but they are error‑prone and should be avoided. You have to assign definitely to a local variable before using it. The compiler raises an error, not a warning, for failure to assign a local variable definitely.


Noted. I don't want to elaborate on this any further, but if you don't mind, can you explain me, why they are exactly error prone? I mean they look fine to me, as I understand it, some elements in your program must always be visible, while others may not.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38461
    
  23
Because global variables permit any code anywhere to change them. Private fields can only be changed via methods, Those methods allow one to restrict changes, so as to maintain the class invariant.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Campbell Ritchie wrote:Have you tried it?

Yes, i have...
May be i have to write "For the following code snippet from your given code:" in my previous post???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38461
    
  23
Shiveen Pandita,
Your post was moved to a new topic.
split from http://www.coderanch.com/t/588466/java/java/Clarification-needed-Local-variable-initialization#2680095 because the direction of the discussion had changed.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Clarification needed on Local variable initialization