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

static reference variable  RSS feed

 
basitha smith
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is diffrence b/w normal reference variable and static reference variable
 
Campbell Ritchie
Marshal
Posts: 55700
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's a static reference variable?

You can have static variables and methods and you can have instance variables and methods. Any member of a class is by default "instance" type, unless you declare it static, but you will often see the term "non-static" used.
You ought to be familiar with instance members. The major differences in static members are:-
  • There is only one of each static member per class.
  • Static members are usually called by the name of the class rather than the name of the object. Look at the Math class; all its members are static and called by writing Math.something().
  • Every instance of the class has access to every static member, but it is always the same static member.
  • . . . but not vice versa. A static member does not have access to any instance member, nor the keywords "this" and (I think) "super."
  • Any changes to a static variable are available to any member, and the value is the same at all times.
  • And there are some minor differences too, eg static members are lost when one serializes an object.

    There is more in your books, the Java Tutorial, etc.

    [edit]I said . . . it is always the same static member. That means, if it is a variable, that this static member has the same value for all instances of the class at any one moment.[/edit]
    [ August 12, 2006: Message edited by: Campbell Ritchie ]
     
    Campbell Ritchie
    Marshal
    Posts: 55700
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . and another feature of static members. They can be used before there are any instances of the class, or even instead of creating an instance.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!