aspose file tools*
The moose likes Beginning Java and the fly likes types of variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "types of variables" Watch "types of variables" New topic
Author

types of variables

Brett Knapik
Ranch Hand

Joined: Oct 15, 2000
Posts: 255
In my books and studies, I keep on coming across different types of variables, like member and instance variables and other types that I cannot think of off the top of my head. Can somebody explain what they mean by this?
------------------
In Gates we trust. Yeah right....


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
huiying li
Ranch Hand

Joined: Feb 12, 2001
Posts: 68
Originally posted by Brett Knapik:
In my books and studies, I keep on coming across different types of variables, like member and instance variables and other types that I cannot think of off the top of my head. Can somebody explain what they mean by this?

A member variable is just a variable in the class. It can be a static variable(belong the the class) or instant variable (belong to each object).
public class MyClass
{
int v = 1; // instance member variable
static int i = 2; // static member variable
public static void main(String[] args)
{
System.out.println("Static variable i = " + i + " can be accessed in a static method");
MyClass c = new MyClass();
System.out.println("Instance variable v = " + c.v + " can be accessed via an object reference");
}
}

SCJP
Becky Miller
Greenhorn

Joined: Jan 24, 2001
Posts: 17
Brett,
An instance variable is a variable that is associated with a particular object , each instance of the class will have its own copy of each of these variables.
A member variable is associated with the class, and is shared by all objects of the class. It is also referred to as class variable because the variable belongs to the class, not to any particular object.
here's an example I found:
public class Sphere
{

// class variable
static double PI = 3.14;

//instance variables
double xCenter;
double yCenter;
double zCenter;
double radius;
}

Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
Brett
Local variables. These are declared within a method.
Member variables. Variables declared within the class body but outside of any methods or constructors are considered Member variables. I believe these are also sometimes referred to as "Class variables." (Someone will correct me if I'm wrong.)
Static variables. These are member variables, but have the "static" declaration with them which means that you can access the variable without making an instance of the class. Also, if there are instances of the class (i.e. objects) the individual objects do not have their own copy of the static variable...only one copy of the static variable is maintained.
Instance variables. These are member variables, but other than that, they are essentially the opposite of Static variables...you can only access them through objects of the class; each object has its own copy of the class Instance variables.
Reference variable. A variable that simply holds a memory address because the variable refers to an object rather than a primitive data type (eg: int, byte, char, boolean, double, etc). Most variables are probably Reference variables.
Please note that this list is probably not exhaustive, and the variable types listed above are not necessarily mutually exculusive.
Allen
[This message has been edited by Allen Alchian (edited February 18, 2001).]


Allen
Brett Knapik
Ranch Hand

Joined: Oct 15, 2000
Posts: 255
Thank you Allen, that is exactly what I was looking for.
------------------
In Gates we trust. Yeah right....
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Just to clear one thing up. Member variables are not "Class variables". "Class Variables" is another name for static variables - they belong to the class, not to any particular object.
For example java.lang.System is a class, and System.out is a public static variable - it is a "class variable" of the class System and can be referred to without an object.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: types of variables