Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaration of variables.

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

check this static variable,methods,blocks








nir
 
nir sharma
Ranch Hand
Posts: 72
Eclipse IDE Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2936
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out this link :-
http://docs.oracle.com/javase/specs/#96595
 
nir sharma
Ranch Hand
Posts: 72
Eclipse IDE Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote: Nir was wrong in saying it was a class variable.

Oops!! My bad.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic