Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

what is instance variable and class variable.

  Asked By: Lorraine    Date: Sep 18    Category: Java    Views: 2081

i am new to java technology....i can define myself as beginner in
java and map my skills to "low"....can anyone give me clear idea of what is
instance variable and class variable.....with simple example program(pls dont
provide me links,as i am new it might be difficult to understand)



6 Answers Found

Answer #1    Answered By: Angelica Ramos     Answered On: Sep 18

I got it..... you want to learn Java, but don't want to read!
Let me see if find just the definition and forward to you.

Answer #2    Answered By: Lonnie Rogers     Answered On: Sep 18

class variables are static variables declared in class  .Those are not belongs
to any particular instance  and can be.
Instance variable  are those non-static member varibles,which are accessed
object refrence.

Answer #3    Answered By: Sumitra 2004     Answered On: Sep 18

Class variables are also known as "static variables." They have the same
value among all instances of a class...considered "shared" variables.
Instance variables belong to specific instances of a class. "Objects"
are what we call instances of a class. Look at the example  below. The
variables and methods marked "static" belong to all Martians. The others
are unique to each individual Martian (alice and marvin).

class Martian {
private static int martianCount = 0;

private String name;
public Martian(String martianName) {
name = martianName;

public static int getMartianCount() {
return martianCount;

Martian alice = new Martian("Alice D. Martian");
Martian marvin = new Martian("Marvin T. Martian");

Answer #4    Answered By: Betty White     Answered On: Sep 18

i probe that "all variables that doesnt have static key word cant be class
variable" am i correct.

Answer #5    Answered By: Beverly Brooks     Answered On: Sep 18

That is correct. Class variables are those identified as "static."

Answer #6    Answered By: Adelfrid Fischer     Answered On: Sep 18

This is a brief explanation of Variables in Java.
Java has three kinds of variables:
1. local (automatic)
2. Instance
3. Class
Local variables are declared within a method body. Both instance  and
class variables are declared inside a class  body but outside of any method
bodies. Instance and class variables look similar, except class variables are
prepended with the static modifier.
Again an instance variable  occurs once per instance of a class; that is, every
time you create a new instance of the class, the system allocates memory for all
of the class's instance variables. This is in constrast to class variables which
occur once per class regardless of the number of instances created of that
class. The system allocates memory for class variables the first time it
encounters the class.

Didn't find what you were looking for? Find more on what is instance variable and class variable. Or get search suggestion and latest updates.