Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

concept of static and instance variable

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- 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

----------- interpret from above result: -------------------------------------------------------------------------------------
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 ?

-----------------------------------------------------------------------------------------------------------------------------------



 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code is too difficult to understand....click on the post button....look for the code tag...clcik it...and put your code inside that code tag...anyhting other than code put it outside the code tag
 
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
As you said i have code the tag?

Let me my explaination about each instance var x or any other is correct ?

Thank you.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
inside the method disp yu are creating a new object....but "x" refers to the current object which is the one you created in the main method...."x" means "this.x"....which means the object which called the current method
 
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
Ok.

What about z

it is static var z =20

it is changed in disp() method with z = 35

This var z is printed in show() method ----z = 35

Thank you.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
girl because it is a static variable.....it belongs to the class...no mater which objject changes its variable....the value of z is the one who changed it the latest
 
Raju Champaklal
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 think instead of reading k&b you should first read head first java...check your local bookstore...you should buy that book if you want to clear scjp with good marks....
 
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
Ok.

I know when a var is declared it belongs to class wherever you called in the class its value is the same unless n untill you changed.

I just got confused in displ () z = 35 ------------ static word was not written with z.

so i asked.

Anyway i ll take your advice.

This what happens when i try to practice code i came to know where i stand in java concepts.

Thank you.
 
Raju Champaklal
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 think dimple this wasnt your code...it was curve's code...try your own code...you will understand better
 
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
yes you are right its curve code.

I was trying to interpret the result of his code.

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is right...but try developing your own code....i think you are very new to java.....go for head first java....
 
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
I am aware about Head First Java

But when thro pdf of this book.

I found starting of the book goes with most of the heavy code.

No doubt expalination technique is excellent.

But for a beginner Lots of code --------- goes hazardous.

So i didnt referred the book.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic