• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

definitions

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
could someone tell me the definitions of the following:
instance variable
class variable
reference variable
static variable
and also please tell the difference between each of these.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can help a little:
class variable and static variable - A class variable is a static variable and vice versa. To declare a class variable, use the static keyword ( static int num = 10; ). A class has only one copy of each class (static) variable. Class variables are shared by all objects of the class.
instance variable - every object has its own copy of instance variables in the class definition. ( int num = 10; ),
reference variable - I'm familiar with the term, but do not use it and cannot remember what to associate it with.
Someone please correct this if necessary. SteveII
 
Trailboss
Posts: 24000
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steven, although your definition of class variable makes a lot of sense, I've always thought of "class variable" to be any variable available to the whole class. Static or not. I thought class referred to the scope. I'm probably wrong.
If I want to talk about what you call a class variable, I always specifically say "static".
To follow this line, I always thought that an instance varibale is a non-static class variable.
Your definittion also makes sense so now I'm not sure what the official line is.
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are probally right, Paul. I have probally been defining them too loosely and have'nt hit a dead end yet. One of the things that intimidated me at first was the fact that Java's jargon was not as cut-and-dry as I like. Your explanation makes sense to me too.
The only thing that makes me think is, quote - "I've always
thought of "class variable" to be any variable available to the whole class. Static or not." In Ivor Horton's book it says,
"A class variable must be declared using the keyword 'static' preceding the type name." But, since I've been thinking about it I remember reading somewhere to think of object when thinking of class. I thought, "yeah right, if a class is an object and an instance is an object, the question of class vs. instance variables is moot.
I gotta say, I am pretty confused too. From now on, I think I'm going to think in terms of static and non-static variables.

[This message has been edited by Steven YaegerII (edited July 31, 2000).]
 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would best define reference variables as opposed to primitive variables.Using that contrast, I would say a reference variable is perhaps a variable that is not a primitive variable. Not so good...
Well, a reference variable represents an object.It is called a reference variable because it holds the address in memory of the object it represents. Thus
String a would hold the address in memory of the String where as int b would hold the actual value of b.
I cant do better...
Herbert.
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to guess something like that but wasn't confident enough. Would it be safe to use this one-liner, "A reference variable doesn't hold an object, but REFERS to it." ???
If that'll work, I'll use it.
SteveII
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class variables (a.k.a. static variables) are "global" to the class, whereas each instance has a copy of its own instance variables. I cannot find any reference in the books I have to contradict this distinction. Au contraire.
The reference variable holds a reference to the object.
Marilyn
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Marilyn,
Do you have a couple lines of code containing a reference variable? Can they be classified as a static variable or instance variable, or are they unique? I'm a little weirded out by them. It sounds like 'this' and 'super' could be reference variables, because they refer to an object.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each variable declaration must have a type. It defines what values that variable can hold. The variable type can be 1) a primitive data type (like int, float or char) which pass copies of the actual value contained in the variable, or 2) the name of a class or interface (like String or Button) which pass copies of the reference (an address known only to the computer) of the object (like Herbert said).
Just like an int or float can be static or not, so also a reference variable of type String or Button can be static or not. If a variable (of either type-primitive or reference) is not declared as static and is outside of all the method brackets {} (which would make it a local variable), then it dafaults to an instance variable which is a variable of each new object.
To get back to your question, I guess that 'this' and 'super' would qualify as reference variables, but you wouldn't have to use modifiers like static because they are predefined when you define the class.
Each object that is instantiated in the class (when you create a new instance) gets its own instance variables, but all the objects of that class "share" the static (class) variable.
Did I help or just muddy the waters?
Marilyn
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephen and Marilyn [and Preeti]
After reading through all the above responses, I would like to TRY and give a clear answer to Preeti's questions... perhaps even clear up what a 'reference' variable is...
There are 3 kinds of variables in Java: Local variables; Instance variables; Class variables (also know as Static variables)
(1)Local Variables
Local variables are created when the method [containing the variable] is
called, and destroyed when the method ends.
Defined: Inside a method... they are LOCAL to that method.
(2)Instance Variables
Each instance of the class [i.e. object] has it's OWN COPY of each instance variable.
The instance variable exists until the program ends, or the object is destroyed.
Defined: Inside a class, but NOT in any method of the class.
(3)Class Variables
A class variable is a variable which belongs to an entire class of objects.
There is only ONE copy of the variable, and every object shares it.
Defined: The same as instance variables, except that the word 'static' appears at the beginning of the definition.
for example,
public class IllistrateVariables{
String anInstanceVariable;
static int aClassVariable;
public static void main(String[] args){
int aLocalVariable;
...
}
}
Now for "REFERENCE VARIABLES"
Personally, I have never heard of these but as far as I make out, reference variables are what I call: "labels". A single object can have many different names, just as your Dad may call you "son" ; your friend may call you "Stephen" ; and your girlfriend may call you "HoneyBunch." Despite all these
names [i.e. labels] that are used to REFER specifically to you, you are constantly the same person[ie object].
So in effect, we could write some code for you:
son.washCar();
Stephen.visit();
HoneyBunch.cookDinner();
Unfortunately, all these REFERENCE VARIABLES [labels] are being use to refer to the same object [you] !! And so you as an object will undergo each method. [assuming you don't 'crash' ]
I hope this cleared things up a bit. Most importantly, Marilyn is this what you were meaning about reference variables???
Please, if there is anything that anybody disagrees about - please say so.
Thank-you.
Michelle

 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was cool Marilyn, it really helped alot. Yours too, Michelle. I'm afraid I am a little shaky on those type definitions still. I have small associations for each one that enable me to tell enough about what they are just by looking at the associations. They helped when I was first starting out, so I have kept them around. Also, alot of times the book definitions spark more questions than they answer. I think now that it is time I started putting my cards on the table and learn when I am wrong.
Thanks, Gals
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice examples, Michelle. My understanding of reference variables is that it is any variable that refers to an object (other than a primitive). To use the example of your class:


public class IllustrateVariables
{
String anInstanceVariable;
static int aClassVariable;
public static void main(String[] args)
{
int aLocalVariable;
...
}
}


"anInstanceVariable" is a reference variable because it refers to a String. "aClassVariable", by contrast, would be a primitive variable as would "aLocalVariable" because they are int's. If one of the variables were, for instance,
Integer number;
it could be an instance variable if it was in position by "anInstanceVariable", or it could be a class variable if it were written as:
static Integer number;
or it could be a local variable if it was placed inside the main() method brackets. It would still be a reference variable in all 3 cases because it is a variable that refers to an object.
The "labels" are actually different names for the object and not variables at all, right?
Marilyn
 
Michelle G
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marilyn
I really am stretching my knowledge resources here... so please once again, if anyone thinks I am wrong - please correct me.
[Oh and also, in my code example above, I just randomly chose the types for the three variables.]
If and only if the variable is NOT of primitive type, then yes it must be referring to an object. And that is why the VARIABLE, anInstanceVariable, is referring to a string and hence acting as a "label".

The "labels" are actually different names for the object and not variables at all, right?
Yes
Well... yes and no A label is a name for an object, but this just means that variables that refer to objects [i.e. what I call labels] are exactly that - VARIABLES.
Sorry, I don't know if this helped - but I tried my best
Michelle

 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, Michelle, I agree with you. Thanks for clarifying that.
Marilyn
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much like drinking squishees too quickly gives me and others brain freeze, so is this thread.
So let's review;
static
class
instance
local
reference
First off 'static' is a keyword that defines a variable much like public/private/protected. So whomever stated static implies scope, you are correct. From this variables with the static modifier are most commonly referred to as class variables.
Class variables, as defined above, is one per class and accessible without an instance, defined with static modifier.
Instance vars do not use the static modifier and are not local. These vars exsist only when the object has been instantiated. Instance vars have visibility issues when dealing with inheritance, but we won't go there. If one thinks that instance vars are class vars when the object is instatiated, you are on a road of confusion. When one creates and object, you can access both class and instance vars, so you may think they are logically equal, they are not. Depending upon visibility, the following code is scary;

Class vars unlike instance vars have a concept of 'global' normally attributed to them (again dependent upon visibility)
Local vars are local to whatever method (and its scope) they are declared.
Reference vars are basically the non-primitive datatypes, objects and arrays. These objects are handled by reference.
Button p,q;
p = new Button (); //p is a new button
q = p; //q is referring to the same button object as p
//iffin I change q, p will change also.
Hopefully I have sufficiently confused each and every one of you in such a manner that a quick trip to the Kwik E-mart is necessary.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class vars != Instance vars
Class vars = static vars
Static - one copy for every instance of an object. If you change a static field within one instance, it will change that field for all of the instances of that object. It enables you to keep a field the same value for all of the instances of an object without having to change each one individually. Also, I've always assumed that you use static fields within static methods, etc.

Yet, I don't see how static has anything to do with visibility. That's a new one on ME.
I have always thought of private, protected, and public, and "package access" (as stated in "Just Java 2", pg. 61) as access modifiers, and static and final more as field modifiers.
 
Bring out your dead! Or a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic