Forums Register Login

Quick question about instance variables

+Pie Number of slices to send: Send
Here is a class:


And an object:


So what exactly are the instance variables in this situation? Size and name? Name is not used again, so I am not even entirely sure why it is used.
+Pie Number of slices to send: Send
not sure why name it there either if it is not used, yes size and name are instance variables because you can call them using one.size, two.size. If size or name was static then they would not be instance variables but merely variables. If I am wrong someone correct me please.
+Pie Number of slices to send: Send
 

Originally posted by James Hambrick:
if size or name was static then they would not be instance variables but merely variables.



Just to confirm, if size and name were static then they would be known as Class variables and would be accessed via the class name. A reference of the Dog Class would not need to be created to use them.

eg:
+Pie Number of slices to send: Send
In class Dog, size and name are instance variables.

In class DogTestDrive, one, two, and three are local variables on the stack.

I've seen this example before and I think it is part of a series of "snippets" and that later a constructor is used to pass in the name when a dog is instantiated.
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 817 times.
Similar Threads
return values in methods
Two classes defined in the same source file ??
Static nad non static Access
If statement now exectutes both if staments but didn't used to
Head First GoodDog code error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:05:51.