Home Tutorial Java Corejava Java static method example

 
 

Java static method example
Posted on: April 9, 2010 at 12:00 AM
This tutorial demonstrate the use of static method and static variable.

Description:

Static variable are used within the static method. Non static variable do not exits in the static method. Static variables are shared by its class instances. Static variable can be used without using the instances of that class.This can be seen in the below program.

Code:

class StaticMethod {
  static int j = 0;

  static void getVariable() {
    j++;
    System.out.println("Value of static variable j is"+j);
  }
}

class TestStatic {
  public static void main(String args[]) {
    StaticMethod.getVariable();
  }
}

Output:

Value of static variable j is 1

Related Tags for Java static method example :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.