• 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

static and instance variables

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here when the instance variable x is called from static method main why does'nt it print instance variables latest value???it prints value 34 y???it should print 98 according to me.....static variables values are changing every time you assign it a new value..but when you call instance variable from the static and a non static method the answer varies for x ...y???this code is made by me....so forgive me in case if i have done anything miserable....thanks.
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did not read your code...it is huge...actually static variables belong to the class....and any object can change the value of the static variable...and the latest value of that static variable will the one it was assigned the latest by any object....rememeber the static variable belongs to the class

while whenever you create new object each object gets its own variable....only and only that object can change its value and not other object....that is why the value of your instance variable is like that.....for example if you create two objects then there will be 2 variables each belonging to each of the objects....but whenever a class is loaded only one static variable is created..but its value can be changed by any object created

i hope this was your question.....reply soon...i am goign to sleep in 5 min
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hey thanks for immediate response...actually in the code static variables are changing when you assign a new value to them and instance variables are also changing the moment you assign a new value to them without using reference variables....when different reference variables are calling the same instance variables the values are different...please tell me why and how does it work??

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, this is how your codes goes ?

-- As you can see st is the first reference variable of class StatInstance.
and the st.x is a static var= 16

-- so whenever you ask x value you get 16.

-- unless and untill you changed the value of x i.e x=34 --------- local var to method disp()
-- then when you ask x value ------------> we get 34

------ show() method is a static method

-------- z is a static var so -------> z = 35

--------- sto is another reference instance variable of class StatInstance with sto.x= 123
-> simple when you called sto.x we get >>>>>>>>>>>>123

-------lets see show1() method

sto.x = 123

- whenever you ask for x value ----------------> 123

in show method last line sto.x-------------> 123

------ as show1 method returns z value
------- z -----------> 120

-----------> in main method when we say st.x------------------> 34

----------- can you interpret from above result: ?



public class StatInstance {
int x=10;
static int z=20;

public void disp(){
z=35;
StatInstance st1=new StatInstance();
System.out.println(x+"here"); ------------>16
st1.x=40;
System.out.println(x+"heresecond"); ------------->16
StatInstance st0=new StatInstance();
x=34;
System.out.println(x+"herethird"); ------------>34
st0.x=89;
System.out.println(x); -------------->34

}
public static void show(){
System.out.println(z); ----------------------> z =35
z=100;

StatInstance st0=new StatInstance();
st0.x=123;
System.out.println(st0.x); ------------> 123
st0.show1();
System.out.println("x is "+st0.x); -----------------> x = 98
}
void show1(){
System.out.println(x); ------------->123
x=98;
z=120;
return;
}
public static void main(String[] args){
StatInstance st=new StatInstance();
Integer i=new Integer(100);
Integer i5=new Integer(200);
System.out.println(i*i5); ------------------> 20000

st.x=16;
System.out.println(st.x); ------------------->16
st.disp();
st.show();
System.out.println(z); ------------------------->120

System.out.println(st.x); --------------------->34
}
}
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hey thanks for your response but i want to know whether why didnt it print the latest value 123..if the latest value of x is changed to 123..why does it print 34??

thanks in advance.....

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

curve karve wrote:

hey thanks for your response but i want to know whether why didnt it print the latest value 123..if the latest value of x is changed to 123..why does it print 34??

thanks in advance.....



Each object created with a ref var has its own instance var. value


Therefore for ref var st0 we have sto.x = 123 so x = 123

And for ref var st we have st.x = 34 so x = 34

let me know whether i am correct ?
reply
    Bookmark Topic Watch Topic
  • New Topic