• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaration of variables.

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

check this static variable,methods,blocks








nir
 
nir sharma
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out this link :-
http://docs.oracle.com/javase/specs/#96595
 
nir sharma
Ranch Hand
Posts: 72
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Oops!! My bad.
reply
    Bookmark Topic Watch Topic
  • New Topic