[Logo]
Forums Register Login
Declaration of variables.


I thought in Java, we must always declare the variables before using them ?

Why in this case, the helloMessage variable can be used before being declared ?
 

WeiJie Lim wrote:

I thought in Java, we must always declare the variables before using them ?

Why in this case, the helloMessage variable can be used before being declared ?



hi weijie.

in java LOCAL VARIABLES should be initialized before using them . for e.g




in your case helloMessage is NOT a local variable. it is a CLASS variable. in case of class variables and instance variables they are given default values , if you don't initialize them. the default values are as follows :

int - 0
float -0.0f
boolean - false
Any Object type - null

Since string is of type Object, it is initialized to null by default.
Oh so for Class variables, we can declare it anywhere we want as it has a default value ?

local variables must be declared before it is used because it doesn't have a default value ?
hi,

check this static variable,methods,blocks








nir
(1 like)
 

WeiJie Lim wrote:Oh so for Class variables, we can declare it anywhere we want as it has a default value ?

local variables must be declared before it is used because it doesn't have a default value ?



It has nothing to do with default value untill unless you are using that variable without initializing it.

example




nir
(1 like)
 

Why in this case, the helloMessage variable can be used before being declared ?


You are confusing the order your code is written in with the order the code is executed in.
Because the helloMessage variable is a static (ie class) variable its declaration is executed the first time the class is loaded and therefore is available when main method is run. Just because it is written after the main method doesn't mean it wont get executed until after the main method has run.
 

Tony Docherty wrote:

Why in this case, the helloMessage variable can be used before being declared ?


You are confusing the order your code is written in with the order the code is executed in.
Because the helloMessage variable is a static (ie class) variable its declaration is executed the first time the class is loaded and therefore is available when main method is run. Just because it is written after the main method doesn't mean it wont get executed until after the main method has run.



Oh thanks alot for the clarification.

What if the variable is a instance variable ? Does the position of the instance variable affect the order the code is executed ? Does the following code work ?

check out this link :-
http://docs.oracle.com/javase/specs/#96595
(1 like)
 

WeiJie Lim wrote:
What if the variable is a instance variable ? Does the position of the instance variable affect the order the code is executed ? Does the following code work ?


Position doesn't matter since its a class variable.
So this code will work.
 

nir sharma wrote:

WeiJie Lim wrote:
What if the variable is a instance variable ? Does the position of the instance variable affect the order the code is executed ? Does the following code work ?


Position doesn't matter since its a class variable.
So this code will work.



Oh crap my bad. I acutally wanted to show an example of an instance variable. Position doesn't affect an instance variable too right ?
 

WeiJie Lim wrote: Position doesn't affect an instance variable too right ?


correct. If it is outside any method, position doesn't matter. It matters inside a method.
 

WeiJie Lim wrote:Oh crap my bad. I acutally wanted to show an example of an instance variable. Position doesn't affect an instance variable too right ?


You did show an example of an instance variable. Nir was wrong in saying it was a class variable.
 

Stuart A. Burkett wrote: Nir was wrong in saying it was a class variable.


Oops!! My bad.

This thread has been viewed 964 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 25, 2018 05:52:51.