Illustrating how functions can change the static variable - Java Object Oriented Design

Java examples for Object Oriented Design:Static

Description

Illustrating how functions can change the static variable

Demo Code

public class Main {
  static int x = 0;

  static void F()
  {/*from ww w.j av  a  2  s.c o  m*/
    x++;
  }

  static void G() {
    --x;
  }

  public static void main(String[] args) {
    F(); // x=1
    G(); // x=0
    F(); // x=1
    System.out.println("value of x:" + x);
  }
}

Result


Related Tutorials