• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Variable initialization

 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This one show output as 0. why?
[ October 05, 2006: Message edited by: Akhilesh Trivedi ]
 
Ranch Hand
Posts: 463
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 Trivedi,

This is because you are initializing j after calling the giveMeJ() method. So by the time you call this method j does not have any value except default value 0. If you change private int j = 10 before calling giveMeJ() it prints 10. Please do verify.

- Surya
 
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you are accessing variable j before its initialization.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a way to get around with the forward reference rule.
What are the forward reference rules?
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Trivedi,

When considering the variables
1- All INSTANCE VARIABLES will have the default value as the zero.

2- All LOCAL VARIABLES (i.e with in the method) should be initialised before use.No default value is allocated for those variables.

If you use the local variables without intialising then you will definitely get error.Similarly if you use the instance variables without intialising then you will not get the error because every instance variables will be having the default values.

Regards,
Ganeshraj S
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Trivedi,

If you wannt to have the value of the variable j as 10 instead of 0 , just add Private static int j = 10; instead of private int j = 10;

and then test . you will get the value as 10.

Thanks and Regards
Anvi
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic