Java static member variable example : Static « Class « Java

Java static member variable example


public class Main {

  public static void main(String[] args) {
    Counter object1 = new Counter();

    Counter object2 = new Counter();

class Counter {
  static int counter = 0;
  public Counter() {
  public int getNumberOfObjects() {
    return counter;


Related examples in the same category

1.Java static method
2.Using Static VariablesUsing Static Variables
3.Static Init Demo
4.Show that you do inherit static fieldsShow that you do inherit static fields
5.Show that you can't have static variables in a method
6.Explicit static initialization with the static clause
7.Static field, constructor and exception
8.This program demonstrates static methods