• 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

Initialization

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

public class Inst{
protected int mystate;
public void go(){
mystate=9;
System.out.print(mystate+" ");
}
public static void main(String args[]){
Inst a=new Inst();
a.go();
System.out.println(a.mystate);
}
}

IN the above code the output of a.mystate is 9. But I think it should be 0 because we assign the value 9 to the variable in the method go(),it is local to that variable. I'm confused of this initialization. Can anyone clear my doubt.

Thanks
sudha
 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all clear one important point that mystate is not local variable, instead it is instance variable. That is every object/instance of Inst has it's own copy of mystate.
EXPLANATION:


  • At Line 1 instance variable has declared, mystateAt Line 5 Object of Inst has created, a.
  • AT Line 6 go() is call.
  • At Line 3 go() is assinged 9.Remember laste value of mystate was 0..Hence all instance varaible are assinged there default values just after the creation of object.That is just after execution of Constructor.
  • At Line 4 mystate is printed as 9.Actually it is called at this Line by compiler as this.mystate.
  • At Line 7 a.mystate is printed as 9.

  • Hope this helps.Any other quey is Wellcome.
    Bye.
    Viki.

    ------------------
    Count the flowers of ur garden,NOT the leafs which falls away!
    [This message has been edited by Vikrama Sanjeeva (edited November 20, 2001).]
    [This message has been edited by Vikrama Sanjeeva (edited November 20, 2001).]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha,
u r confusing with local variable and instance vaiable.
when instance variable is declared , it is assigned a value 0 (if is in integer) , and u can access and change the value of this varaible any where in the code.
satya

Originally posted by sudha siva:
Hai,

public class Inst{
protected int mystate;
public void go(){
mystate=9;
System.out.print(mystate+" ");
}
public static void main(String args[]){
Inst a=new Inst();
a.go();
System.out.println(a.mystate);
}
}

IN the above code the output of a.mystate is 9. But I think it should be 0 because we assign the value 9 to the variable in the method go(),it is local to that variable. I'm confused of this initialization. Can anyone clear my doubt.

Thanks
sudha


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic