Publish Your Article Here

Program: Example for static variables and methods.


In java, static belongs to class. You can create static variables and static methods. You can call these directly by using class name, without creating instance.

Java static variables:

Static variables are belongs to the class and not to the object. These are only once, at the starting of the execution. Static variables are not part of object state, means there is only one copy of the values will be served to all instances. You can call static variable with reference to class name without creating an object. Below example shows how to create and call static variables.
Java static methods:

Static methods are also similar to static variables, you can access them with reference to class name, without creating object. Inside static methods, you cannot access instance variables or instance methods. You can only access static variables or static methods.

package com.java2novice.staticexmp;

public class MyStaticMethods {

        private String name;
        private static String staticStr = "STATIC-STRING";
        public MyStaticMethods(String n){
       = n;
        public static void testStaticMethod(){
                System.out.println("Hey... I am in static method...");
                //you can call static variables here
                //you can not call instance variables here.
        public void testObjectMethod(){
                System.out.println("Hey i am in non-static method");
                //you can also call static variables here
                //you can call instance variables here
                System.out.println("Name: ";
        public static void main(String a[]){
                //By using class name, you can call static method
                MyStaticMethods msm = new MyStaticMethods("Java2novice");

Hey... I am in static method...
Hey i am in non-static method
Name: Java2novice
 Next Program >>

List Of All Static Keyword Programs:

  1. Example for static variables and methods.
  2. Example for static block.
  3. Example for static block vs constructor.
  4. Example for singleton class using static block.
Knowledge Centre
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
The very best thing you can do for the whole world is to make the most of yourself.
-- Wallace Wattles

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2024 by Nataraja Gootooru. All Rights Reserved.