The Artima Developer Community
Sponsored Link

Java Answers Forum
Use of Static Variables

3 replies on 1 page. Most recent reply: Mar 18, 2004 12:45 PM by Scuba Steve

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
mudit

Posts: 10
Nickname: mbhandari
Registered: Feb, 2004

Use of Static Variables Posted: Mar 18, 2004 1:58 AM
Reply to this message Reply
Advertisement
What is the use of Static Variables and why are they being used


Viswanatha Basavalingappa

Posts: 84
Nickname: viswagb
Registered: Nov, 2003

Re: Use of Static Variables Posted: Mar 18, 2004 5:42 AM
Reply to this message Reply
Hi,

Static variables are those that are declared static during declaration.

Example:

class VariableTest
{
int temporary = 1;
static int permament = 1;
}

Variables declared static are commonly shared across all instances of a class.

Variables declared static are commonly shared across all instances of a class. When you create multiple instances of VariableTest class –This variable permament is shared across all of them. Thus, at any given point of time, there will be only one string value contained in the permament variable.

Since there is only one copy of the variable available for all instances, the code this.permament will result in compilation errors – because it can be recalled that this.variablename refers to the instance variable name. Thus, static variables are to be accessed directly, as indicated in the code.


Hope this helps you to understand

twc

Posts: 129
Nickname: twc
Registered: Feb, 2004

Re: Use of Static Variables Posted: Mar 18, 2004 5:46 AM
Reply to this message Reply
Here is an example that might clarify the situation. Imagine that you are creating a game based on the movie 101 Dalmations. As part of that project, you create a Dalmation class to handle animating the various Dalmations. The class would need instance (non-static) variables to keep track of data that is specific to each Dalmation: what its name is, how many spots it has, etc..

But you also need to be able to keep track of how many Dalmations have been created so you don't go over 101. That can't be an instance variable because it has to be independent of specific Dalmations. For example, if you haven't created any Dalmations, then this variable has to be able to store zero. Only static variables exist before objects are created. That is what static variables are for - data that applies to something that is beyond the scope of a specific instance of the class.

twc

Scuba Steve

Posts: 4
Nickname: scubasteve
Registered: Jul, 2003

Re: Use of Static Variables Posted: Mar 18, 2004 12:45 PM
Reply to this message Reply
Nice explanation, but I would modify the statement "data that applies to something that is beyond the scope of a specific instance of the class" to be something more like "data that applies to the class - not an instance of the class". One need not have any instances of a class to use static variables.

The Singleton design pattern provides another example of why such variables are useful.

e.g.,


public class ScubaSteve {

// static private instance reference to the single instance of this class
static private final ScubaSteve instance = new ScubaSteve();

// private constructor
private ScubaSteve() {}

// static public instance accessor method
static public ScubaSteve getInstance() {
return ScubaSteve.instace;
}
}


cheers,
Steve

Flat View: This topic has 3 replies on 1 page
Topic: adding elements od an array Previous Topic   Next Topic Topic: please help get compiler error

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use