AP Computer Science --- Haas --- StaticExamples
The following are examples of static variables and methods.
/**
* >>>>>>>>>>>>>>>>>>>> static fields <<<<<<<<<<<<<<<<<<<
* When you declare a field variable of a class as static,
* that field variable is instantiated only once, no matter how many instances
* of the class are created.
* In other words, a static field variable is a class
* variable, and, not an instance variable. If the value of a static field
* variable of a class is changed by an instance of that class, the value of
* that field variable changes for all instances of that class.
*
* >>>>>>>>>>>>>>>>>>>> static methods <<<<<<<<<<<<<<<<<<<
* When you declare a method of a class as static, that method becomes a class
* method operating in the class itself rather than in instances of the class.
* A static method can only refer to static field variables and static methods
* of the class. A static method is implicitly final, i.e. it cannot be overriden
* by subclasses of the class.
*
* >>>> Copy into BlueJ and run, make sure you understand how it works! <<<<
*/
class Stock {
private static double pricePerShare;
private int numberOfShares;
private double totalValue;
Stock (int shares, double pps) {
numberOfShares = shares;
pricePerShare = pps;
}
public double getNetWorth() {
return numberOfShares * pricePerShare;
}
public static double getPricePerShare() {
return pricePerShare;
}
}
public class StockTester {
public static void main (String[] argsIn)
{
Stock joesStocks = new Stock(10,15);
System.out.println("joes net worth = " + joesStocks.getNetWorth());
System.out.println("joes price per share = " + joesStocks.getPricePerShare());
Stock jimsStocks = new Stock(2,20);
System.out.println("jims net worth = " + jimsStocks.getNetWorth());
System.out.println("joes net worth = " + joesStocks.getNetWorth());
System.out.println("jims price per share = " + jimsStocks.getPricePerShare());
System.out.println("joes price per share = " + joesStocks.getPricePerShare());
System.out.println("price per share = " + Stock.getPricePerShare());
}
}
/**
* A class with Instance and Static methods.
*
* Instance methods rely on the state of a specific object instance.
* Instance methods are tied to a particular instance because the
* behavior that the method invokes relies upon the state of that
* particular instance.
*
* When you declare a method as static, you define that method as
* being a class method. A class method applies to the class as opposed
* to any particular instance. The behavior instigated by a class method
* does not rely on the state of a particular instance. In fact, a static
* method cannot rely on an instance's state since static methods lack
* access to this reference. Instead, the behavior of a class method
* either depends on a state that all objects share at the class level,
* or is independent of any state at all.
*/
public class MrHappy
{
private String mood = "happy";
private static int happyCount = 0;
public MrHappy() {
happyCount++;
}
public static int instances() {
return happyCount;
}
public String getMood() {
return mood;
}
public void receivePinch() {
if(mood.equals("happy")) {
mood = "annoyed";
}
else {
mood = "angry";
}
}
public void receiveHug() {
if(mood.equals("angry")) {
mood = "annoyed";
}
else {
mood = "happy";
}
}
}
/***********************************************************
* MrHappyTester
**********************************************************/
public class MrHappyTester
{
public static void main(String[] args)
{
MrHappy obj1 = new MrHappy();
System.out.println( "\nFirst MrHappy");
System.out.println( "I am " + obj1.getMood());
obj1.receiveHug();
System.out.println( "After Hug I am " + obj1.getMood());
MrHappy obj2 = new MrHappy();
System.out.println( "\nSecond MrHappy");
System.out.println( "I am " + obj2.getMood());
obj2.receivePinch();
System.out.println( "After Pinch I am " + obj2.getMood());
System.out.println( "\nMrHappy Count = " + obj1.instances());
System.out.println( "\nMrHappy Count = " + obj2.instances());
System.out.println( "\nMrHappy Count = " + MrHappy.instances());
}
}