• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Java: Static Variables, Static Methods and Static Constructor

Java: Static Variables, Static Methods and Static Constructor

May 6, 2009 by Sze Hau 1 Comment

Java’s static variables are variables or fields that are common to all object of a class. Unlike instance variables, objects of the same class have their own distinct copies of the variables and values. In other words, a static variable is shared among objects of the same class. You can declare static variables (or static fields or class variables) by adding static modifier to the variables declaration.

For example:

public class Account {

  // Account balance
  private int balance;

  // Number of account in the system
  private static int numberOfAccounts = 0;
  ......
}

Instances (objects) of class Account will have their own copy of balance but all account objects share the same numberOfAccount.

Java’s static methods or class methods are methods that can be called without instantiate an object. Static methods also have static modifier to the methods declaration. Although you can call static methods with an object reference but it is not recommended as  it does not make it clear that they are class methods. Static methods cannot access to instance variables and methods. A common use of static methods is to access to the static variables of it’s class.

For example:

public class Account {

  // Account balance
  private int balance;

  // Number of account in the system
  private static int numberOfAccounts = 0;

  // Constructor
  public Account(int balance) {
    this.balance = balance;
    // Calling static method
    incrementNumberOfAccounts();
  }

  // Close account
  public void close() {
    decrementNumberOfAccounts();
  } 
  // Get number of accounts in the system
  public static int getNumberOfAccounts() {
    return numberOfAccounts;
  }

  // Incement number of account
  private static void incrementNumberOfAccounts()
    numberOfAccounts ++;
  }

  // Decrement number of account
  private static void decrementNumberOfAccounts()
    numberOfAccounts ++;
  }

  ......
}

From the above example you can know the number of account objects have been created by calling Account.getNumberOfAccounts().

Java’s static constructor or static initialization block  is a normal block of code enclosed in braces, { }, and preceded by the static keyword. The block of code can be appeared in any place of the class body. Like a normal constructor, it is used to initialize static variables. Although you can initialize a static variable inline, but sometime you may need more codes for more complicated situation to initialize the static variables.

For example:

 

public class Jackpot {

  // Jackpot value
  private int value;

  // Extra jackpot value, with 
  // 70% of chances to have zero values and
  // 10% of chances to have 100 and
  // 10% of chances to have 1000 and
  // 10% of chances to have 10000 and
  private static int extra;

  // Static constructor
  static {
    Random random;

    random = new Random();
    switch(random.nextInt(10)) {
    case 7:
      extra = 100;
      break;
    case 8:
      extra = 1000;
      break;
    case 9:
      extra = 10000;
      break;
    default:
      extra = 0;
    }
  }

  ......
} 

 

Do remember that, Java’s  static initialization blocks are called in the order that they appear in the source code.

More from my site

  • Java: Static Initializer (Static Constructor)Java: Static Initializer (Static Constructor)
  • Java Static ConstructorJava Static Constructor
  • Java: Unlimited Strength Jurisdiction PolicyJava: Unlimited Strength Jurisdiction Policy
  • Java: How To Select Top N Objects From A ListJava: How To Select Top N Objects From A List
  • Java: How To Implement ungetc in JavaJava: How To Implement ungetc in Java
  • Java: How To Use SQL LIKE Clasue with PreparedStatement?Java: How To Use SQL LIKE Clasue with PreparedStatement?

Filed Under: Programming and Scripting Tagged With: Java, static constructor, static methods, static variables

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Trackbacks

  1. Java: Static Initializer (Static Constructor) says:
    February 10, 2014 at 6:44 pm

    […] Java programmers, including my self, might have mistaken that the portion of code that initializes the static variables of a class […]

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in