====== Static Class Variables and Methods, What's This? ======
----
===== Static Class Variables and Methods =====
* Static class members (Class variables and methods that are declared //static//)
* Belong to the class itself and are __independent of any one instance (object) of the class.__
* //Instance// variables belong to //instances// of the class, i.e., objects of the the class.
* //Instance// methods belong to //instances// of the class.
* Can't call an //instance// method without creating an instance (object) of the class first.
MagicEightBall m8 = new MagicEightBall(); // create MagicEightBall instance (object) m8
System.out.println("Your answer: " + m8.getAnswer(); // call the getAnswer() instance method of m8
* Static variables and methods are //non-instance// variables and methods.
* If you want all the instances of a class to share data, use //static variables// (or static constants), which are also known as //class variables.//
* This allows for useful public static constants in Java such as //Math.PI//.
* //Static methods// can be called without creating an instance (object) of the class.
* This allows for useful methods in Java such as the static methods of the //Math// class, e.g. //Math.random()//, //Math.sqrt()//, //Math.pow()//, etc.
* After all, it would be inconvenient to have to create //Math// class objects just to use methods like //random()// and //sqrt()// or constants like //PI//.
==== Static class variables ====
* A static class variable is added using the //static// keyword.
* Point/Rectangle/TestRectangle static example:
* In the example, a static int //pointCount// variable is added to a //Point// class:
public class Point {
private int _x;
private int _y;
// Use static pointCount variable to track number of Point objects created:
private static int pointCount = 0;
// Constructors: static pointCount gets incremented in every constructor:
public Point( int x, int y ) {
setX( x );
setY( y );
pointCount++;
}
public Point() {
pointCount++;
}
...
...
// Instance methods can access static variables
public int getPointCount() {
return pointCount;
}
* Also, a static //rectangleCount// variable is added to a //Rectangle// class that includes the above //Point// class:
public class Rectangle {
private Point _point;
private int _width;
private int _height;
// Use static rectangleCount variable to track number of Rectangle objects
// created:
private static int rectangleCount = 0;
// Constructors: static rectangleCount gets incremented in every
// constructor:
public Rectangle( int point_x, int point_y, int width, int height )
{
_point = new Point( point_x, point_y );
// Must call Point class constructor to create _point
setWidth( width );
setHeight( height );
rectangleCount++;
}
...
...
// Instance methods can access static variables
public int getRectangleCount() {
return rectangleCount;
}
* In //TestRectangle.java//, the //pointCount// and //rectangleCount// static variables are accessed to show how static class variables work:
// Use Point class static pointCount variable:
System.out.println( box.getPoint().getPointCount() +
" points so far." );
// Use Rectangle class static rectangleCount variable:
System.out.println( box.getRectangleCount() +
" rectangles so far." );
==== Static class methods ====
* When a class contains a //static// method, an instance (object) of the class is not needed to use the method.
* For example, with a //static// version of the //getPointCount()// method called //sgetPointCount()//), we can access the //pointCount// static variable using //Point.sgetPointCount()//.
* The "Point" in //Point.sgetPointCount()// is the class name, not the name of a //Point// class object.
* **Restriction:** With a static method, you can only access static variables of the same class.
public class Point {
...
private static int pointCount = 0;
...
...
// static version of getPointCount:
public static int sgetPointCount() {
return pointCount;
}
...
}
public class TestRectangle {
...
...
// Use Point class' static sgetPointCount method:
System.out.println( Point.sgetPointCount() + " points so far." );
...
...
}
----
===== What's "this"? =====
----
* The "this" reference variable
* "this" is the name of a reference variable that an object can use to refer to itself.
* Can be used in only non-static (instance) methods.
* Common use of "this" is to overcome "shadowing" in "set" methods:
* See http://www.leepoint.net/data/variables/60shadow-variables.html.
public class Rectangle {
private int width;
private int height;
// setHeight method of Rectangle class:
public void setHeight( int height ) {
height = height; // ??? Shadowing: Which height is which ???
}
}
public class Rectangle {
private int width;
private int height;
// Avoid shadowing using "this":
public void setHeight( int height ) {
this.height = height;
// "This" Rectangle's instance variable height
// is equal to the value of the input parameter
// variable height.
}
}
----