• 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

class-Vs-instance vars ..why initialized....???

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know class(member) variables are initialized to a initial value but not instance variables,....but why..?
can anybody pls explain the reason behind this.
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand you correctly here you are making a distinction between static and non-static variables. Am I right? Any class variables whether static or non-static(instance) are guaranteed to be initialized. Only the variables that are defined locally to a method are the ones which are not provided a default initial value. Java does this to make sure that the variables are properly initialized before you get your hands onto any variable. That is why you don't see strings in int which was the case with earlier programming language like c and cobol.

Originally posted by srinivas bolloju:
i know class(member) variables are initialized to a initial value but not instance variables,....but why..?
can anybody pls explain the reason behind this.


 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi anshul can u be a bit more clear, what im trying to understand is when java initialises the class variables ,then why it doesnt initialize instance variable...
 
Anshul Manisha
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do you mean when static(class) variables are initialized why instance variables are not initialized? Static variables can be used without the instantiation of class. So if unless you were to create an object of class you would not have any object to which to tie the instance variables to. However if you create an object of class, static variables will be intitialized first and then instance variables. Rest assured that Java will make sure that the variables you use have been properly initialized before you get your hands on them. It will do so either by forcing you to do it(compiler error if you don't intialized method variables) or initializing the variables to their default values. Hope I make more sense now.
 
srinivas bolloju
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you
reply
    Bookmark Topic Watch Topic
  • New Topic