Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

class variables and instance variables  RSS feed

 
kapokia Lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when I am studying "SCJP Tiger Study Guide", I am confused about a sentence:
>Field declarations describe class variables, which are incarnated once, and instance variables, which are freshly incarnated for each instance of the class.
The URL is http://java.boot.by/scjp-tiger/ch01.html.

I wrote a letter to the writer of the article, Mr.MZ (Maybe a warm-hearted team? ^_*). Mr.MZ replied me recently, and advised me to write the question at this forum. I also got a chief description about class variables and instance variables:
>there is only 1 static (class) variable per class
>there is 1 instance variable per class instance (object)

It seemed that I mistook the meaning of class variables and instance variables. Could someone give me some examples for class variables and instance variables?

Thanks a lot!
 
Chris Wox
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class variable is a static variable in some class. You might better call them perClass variables since there is only one copy of this variable to serve all objects of the class, not one per object. It is allocated when the class is first loaded.

instance variable a variable, part of an Object. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new.
[ May 25, 2006: Message edited by: Chris Wox ]
 
Chris Wox
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both types of variables has a different scope. A variable's scope defines how long that variable lives in the program's memory and when it expires. Once a variable goes out of scope, its memory space is marked for garbage collection by the JVM and the variable may no longer be referenced.


# Instance variables are in scope as long as their enclosing object is in scope. An instance variable is a part of the object that contains it and cannot live independently of it. Each instance of an object has its own version of instance variables.

# Class variables are in scope from the point the class is loaded into the JVM until the the class is unloaded. Class are loaded into the JVM the first time the class is referenced: often when the first object of that class in instantiated with the new operator. They are the most long-lived kind of variable and come the closest to being a global variable. A class variable's value is shared by all instances of that class. So, even if there are ten objects that contain a class variable named todaysDate, the value that todaysDate contains is shared by all ten objects--there is only one todaysDate.
 
kapokia Lin
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
Is the example I created correct:

class A {
  int iA0 = B.iB0; // reference for B's class variable
  int iA1 = new B().iB1; // reference for B's instance variable
}

class B {
  public static int iB0;
  public int iB1;
  void mthd1() {
    int iB2; // instance variable, other classes cannot get the reference for iB2
  }
}
[ May 26, 2006: Message edited by: kapokia Lin ]
 
Chris Wox
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

void mthd1() {
int iB2; // instance variable, other classes cannot get the reference for iB2}
}

[ May 26, 2006: Message edited by: kapokia Lin ][/QB]


iB2 here is local variable (automatic variable) , remember there are three
kinds of variables in Java ( in terms of scope ) class variable , instance variable ( fields) and local (automatic variable)

local variables are declared in the method and are accessible only
within that block in which they are declared . they must be initialized before they are referenced as they dont have a default value
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!