wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Declaration of variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declaration of variables." Watch "Declaration of variables." New topic
Author

Declaration of variables.

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85


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 ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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.
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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 ?
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

hi,

check this static variable,methods,blocks








nir
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

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
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
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.
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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 ?

shivamahesh bachu
Greenhorn

Joined: Sep 14, 2012
Posts: 13
check out this link :-
http://docs.oracle.com/javase/specs/#96595
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

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.
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
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 ?
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

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.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

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

Oops!! My bad.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaration of variables.
 
Similar Threads
Innner class in Interface
when does a variable is initialized ?
Variables in Java
Using a variable before it has been initialised
Why a final instance variable needs to be initialized ?