Java static Variable Method


Java static Variable Method

Summary: By the end of this tutorial "Java static Variable Method", you will comfortable to write programs with static variables and methods.

After discussing the usage of "this" keyword, let us move to another keyword "static". The "static" keyword is used as an access modifier. Java has access specifiers and access modifiers and both are very different. Access specifiers specify the access and access modifiers modifies the access. It may be confusing right now and will be clear soon.

Generally, you know earlier, to call a method and also a variable from the static main(), you require an object. Of course it is right but not with static methods and static variables. static modifier modifies the access and allows to call without the help of an object. "static" keyword can be applied to instance variables and methods and not with classes and also not with local variables..

Static with Variables

Java permits to call a static variable or method in three ways.

  1. Can be called without the help of an object
  2. Can be called with the help of an object
  3. Can be called with class name

The following program explains the static keyword with variables.

Example on static variables usage (of Java static Variable Method)
public class StaticDemo
{
  static int price = 100;
  public static void main(String args[])
  {
    System.out.println(price);                         // without object

    StaticDemo sd1 = new StaticDemo();
    System.out.println(sd1.price);                     // with object

    System.out.println(StaticDemo.price);              // with class name
   }
}
Java static Variable Method
Output screen of StaticDemo.java

The instance variable price is declared as static and is called in three ways – without object, with object and with class name.

Example on Static methods usage (of Java static Variable Method)

The same style of static variables is followed with static methods also and can be called in three ways. Following program explains.

public class StaticDemo2
{
  public static void show()
  {
    System.out.println("Hello World");
  }
  public static void main(String args[])
  {
    show();                                              // without object

    StaticDemo2 sd1 = new StaticDemo2();             
    sd1.show();                                          // with object

    StaticDemo2.show();                                  // with class name
  }
}
Java static Variable Method
Output screen of StaticDemo2.java

The show() method is declared as static and is called in three ways. It is allowed with class name also to facilitate to call many methods of Java API classes without the help of an object. Many methods are declared static in Java classes like java.lang.Character and java.lang.Math etc.

What is Class Variable? (of Java static Variable Method)

A static variable is also known as "class variable" as all the objects of the whole class refer (or share) the same variable (location); that is, one variable is used by all the objects (in encapsulation, each object will have separate location of a variable). As all the objects refer the same location (variable), no encapsulation exist with static variable.

Can a local variable be declared static? (of Java static Variable Method)

A local variable cannot be static. If static the meaning of static is lost. A static variable scope is for the objects of whole class and if declared local as static, the purpose of static is lost and compiler raises error.

Why the main() is static? (of Java static Variable Method)

main() method is declared as static to allow the JVM to call the main() without the help of an object. JVM is a separate process and to call the method of another process (your program), it requires an object. As the execution starts from the main(), the JVM cannot create an object without entering the main() method. If the main() method is allowed to call without the need of an object, the JVM can create hundreds of objects once enters the main(). For this reason, main() is declared as static.

Memory Allocation for static Variables

We know earlier every object will have its own copy of variables. But with static variables, it is very different. A static variable location is shared (or accessed or pointed) by all the objects of the class. The result is, if one object changes the value, all the other objects also get affected. With static variables, no encapsulation exists. Observe the following program.

public class Demo
{
  static int x = 10;
  public static void main(String args[])
  {
    System.out.println(x);                       // 10

    Demo d1 = new Demo();
    Demo d2 = new Demo();

    System.out.println(d1.x);                    // 10
    System.out.println(d2.x);                    // 10
 
    d1.x = 20;
    System.out.println(d2.x);                    // 20

    d2.x = 30;
    System.out.println(d1.x);                    // 30

    System.out.println(x);                       // 30
  }
}
Java static Variable Method
Output screen of Demo.java

Variable x is declared as static. Objects d1 and d2 point to the same x location. Observe, if d1 changes d2 and if d2 changes d1 are getting affected. Because all the objects of the whole class share the same location, the static variable is known as "class variable".

25 thoughts on “Java static Variable Method”

  1. Thanx a lot sir , reallllly u have done superb explanation here. I’ve ever got these kinds of simply knowledge of complicated topics.
    Always HELP with this kind of efforts to us.

  2. Sir, whr does static methods and static variables are stored in memory…if its jvm specific then i m talking about sun specific

  3. Sir, i am still confused why main() is static?If the main() method is allowed to call without the need of an object, the JVM can create hundreds of objects once enters the main(). For this reason, main() is declared as static.

    i can’t understand this things.please help me sir.

    1. Your program, say, a Demo program and JVM are two separate processes in the RAM. We know execution starts from main() method. To call the main(), JVM requires an object because JVM is an outside (separate) process not connected to Demo process. So to call main(), JVM requires an object. How JVM gets an object of Demo unless it enters into the execution space of Demo. The execution space of Demo contains main(). For this reason, main() is declared static to allow the JVM to call main() without the need of an object.

      Still you do not understand, take the help of colleague programmer or if you are at Hyderabad, meet me.

      1. Thanks a ton sir, why main() is static? now everything is clear.
        Sir, could you suggest me some exercise questions for practice.

  4. Hi,

    I am not clear with the transient and volatile keyword……Could you please explain it in a clear cut way??? Waiting for your valuable replies………

    Regards,
    smpth

    1. Static, in Java, is known as access modifier. Access modifier modifies the access. Let us see what it modifies. A static variable and a static method can be called without the help of an object and moreover, a static variable will have only location that can be accessed by all the objects of the class. Local variable scope and life is within the method where it is declared. How can a local variable will have only one location that is accessed by all objects? To use a class, its object is to be created with new keyword. How can you call a method without the help of an object? Object identifies for which class the method belongs. JVM searches that class and loads it.

      1. OK tanq sir,the static variable and static method can be called with the help of an object and with out object also then what is the difference between these two callings(with object and with out object)

Leave a Comment

Your email address will not be published.