Java

Static Variables and Methods

WebDevLee 2022. 2. 9. 14:16

자바의 Static한 변수와 메소드에 대해 정리하였습니다.

 

 


< Static Methods >

Static methods are methods that belong to an entire class, not a specific object of the class. Static methods are called using the class name and the . operator.

 

Example)

double randomNumber = Math.random();
// Stores a random decimal between 0.0 and 1.0 in randomNumber
 
double number = Double.valueOf("2.5");
// Transforms the String "2.5" into a double

: We didn’t need to create a object in order to use that method.

 

 


< Static Variables >

Much like static methods, you can think of static variables as belonging to the class itself instead of belonging to a particular object of the class.

Just like with static methods, we can access static variables by using the name of the class and the . operator. 

 

  • Unlike static methods, you can still access static variables from a specific object of the class. However, no matter what object you use to access the variable, the value will always be the same.

 

Example)

public class Dog{
  // Static variables
  public static String genus = "Canis";
 
  //Instance variables
  public int age;
  public String name;
 
  public Dog(int inputAge, String inputName){
    this.age = inputAge;
    this.name = inputName;
  }
  
  public static void main(String[] args){
    System.out.println(Dog.genus); // Prints Canis
  }
}

// always same!
public static void main(String[] args){
  Dog snoopy = new Dog(3, "Snoopy");
  Dog ringo = new Dog(5, "Ringo");
 
  System.out.println(Dog.genus); // Prints Canis
  System.out.println(snoopy.genus); // Prints Canis
  System.out.println(ringo.genus); // Prints Canis
}

 

 


< Modifying Static Variables >

Often times, you’ll see static variables used to keep track of information about all objects of a class.

 

Example : our variable numATMs is keeping track of the total number of ATMs in the system. Similarly, totalMoney variable is keeping track of all money across all ATMs.

public class ATM{
  // Static variables
  public static int totalMoney = 0;
  public static int numATMs = 0;

  // Instance variables
  public int money;

  public ATM(int inputMoney){
    this.money = inputMoney;
    numATMs += 1;
    totalMoney += inputMoney;
  }

  public void withdrawMoney(int amountToWithdraw){
    if(amountToWithdraw <= this.money){
      this.money -= amountToWithdraw;
      totalMoney -= amountToWithdraw;
    }
  }

  public static void main(String[] args){
    System.out.println("Total number of ATMs: " + ATM.numATMs); 
    ATM firstATM = new ATM(1000);
    ATM secondATM = new ATM(500);
    System.out.println("Total number of ATMs: " + ATM.numATMs); 

    System.out.println("Total amount of money in all ATMs: " + ATM.totalMoney);  
    firstATM.withdrawMoney(500);
    secondATM.withdrawMoney(200);
    System.out.println("Total amount of money in all ATMs: " + ATM.totalMoney);    
  }
}

// Total number of ATMs: 0
// Total number of ATMs: 2
// Total amount of money in all ATMs: 1500
// Total amount of money in all ATMs: 800

 

 


< Writing Your Own Static Methods >

One important rule to note is that static methods can’t interact with non-static instance variables.
This is due to static methods not having a this reference.

 

To wrap your mind around this, consider why we use this when working with non-static instance variables.
Let’s say we have a Dog class with a non-static instance variable named age. If we have a line of code like this.age = 5; that means we’re setting the age of a specific Dog equal to 5. However, if age were static, that would mean that the variable belongs to the entire class, not a specific object.


The this keyword can’t be used by a static method since static methods are associated with an entire class, not a specific object of that class. If you try to mix this with a static method, you’ll see the error message non-static variable this cannot be referenced from a static context.