What do you mean by static in Java?

When I was learning Java, I always get confused what static variables and static methods mean. I read a lot of books explaining static and still it’s not clear to me. Let’s consider the “Hello World” class and program in Java.


// HelloWorld.java
public class HelloWorld {
	public static void main (String args[]) {
		System.out.println ("Hello World!");
	}
}

and consider the following class,


// HelloWorld.java
public class HelloWorld {
	public void main (String args[]) {
		System.out.println ("Hello World!");
	}
}

Are these two classes the same?  Of course, they are not the same. The first class has a static keyword in its main method and the other one is simply a method without this keyword static.As always Java follows this signature in the first class. And we consider this as a complete and a running program in Java. The static main method means that all objects created out from this class has only one method main. And you can call main method without instantiating the class and directly invoke this method with the name of the class (HelloWorld.main()).All instance variables and methods of the class that are declared as static are global variables or global methods. And we can call them directly without creating an object from a class by putting it’s class name in front of a variable and a method name in between the dot operator.Let’s consider a class counter example. We define a class, Song, and define the global variable count that increments every time we create an instance of the class, Song.


// Song.java
public class Song {
	private String title;
	// global variable
	public static int count;

	public Song (String title) {
		this.title = title;
		// Increment class variable count every time song is created.
		Song.count++;
	}

	public String toString () {
		return title;
	}
}

// SongDriver.java
public class SongDriver {
	public static void main (String args[]) {
		Song hello = new Song ("Hello");
		System.out.print ("Song " + Song.count + ": ");
		System.out.println (hello);
		Song elMundo = new Song ("El Mundo");
		System.out.print ("Song " + Song.count + ": ");
		System.out.println (elMundo);
	}
}

The program simply increments count when the hello object is created. And it increments again when elMundo is created. And we display the value of count by invoking count with it’s class name, Song.count.Therefore, when we say static, it is global and we can also call them class variable or class methods.

8 Comments

  1. pepestudios said,

    November 17, 2007 at 5:46 am

    Some important things to note on Java’s static modifier:
    1. Sometimes its not safe to use static functions or variables on a multi-threaded java program. This is because static functions and variables are made global. Thus java threads should at least need to provide mutex for these functions and variables.

    2. Static variables and functions are inherited but not in a strong sense as how instance variables and functions are. At runtime you may explicitly call a function or reference a variable from the heirarchy of classes that inherited and overriden it. For example, there is a base class called Animal with a static member variable called eat(). A class called Snake is subclassed from class Animal and it also overrides function eat(). At runtime you may call Animal.eat() to get the base class’s eat() function and Snake.eat() to Snake class’s eat function. See the difference? If eat() is not static you can’t explicitly call eat(), you need to instantiate an object to access it. Calling eat() from a Snake object will call the overriden eat() function if eat is not static.

    3. Java’s static modifier may also declare static nested classes. Wherein these classes(inner class) are declared inside a class(enclosing class) without having a relationship with the enclosing class.

  2. aradhana singh said,

    February 11, 2008 at 9:29 am

    you didn’t decleared the main meaning of static?

  3. aradhana singh said,

    February 11, 2008 at 9:30 am

    so plz declear it immeadetly….

  4. Blink said,

    February 14, 2008 at 11:52 am

    If the member variables and methods of a class are static, meaning, all objects that are instance of that class shares that variables and methods. And those static variables and methods can be used or invoked without instantiating that class that where they belonged. They can be invoked using the class name (dot) variable or method.

    Consider the following example:

    
    Song s1 =new Song ("s1");
    Song s2 =new Song ("s2");
    Song s3 =new Song ("s3");
    Song s4 =new Song ("s4");
    
    System.out.println ("There are " + Song.count + " created.");
    
  5. Demonray said,

    February 25, 2008 at 2:28 pm

    Why is main static in java?

  6. Blink said,

    March 2, 2008 at 3:48 pm

    main() is a special method in Java where your program basically start. It should always static because all instantiated objects out from a class should have always only one method. They all shares one main method because it will not have different main to be executed.

  7. Stephen Akandwanaho said,

    March 7, 2008 at 11:14 am

    In the context of JAVA, static means Unique. You can not have two main methods in a program( I think), It is unique to that particular program. Actually, at this point of the program, it’s where the execution starts.

  8. Blink said,

    March 10, 2008 at 3:20 am

    You are right Stephen. static could also mean UNIQUE because every class will shares it. And also, there should always be one and only main method. But I think, main as identifier is not reserved. I tried it once in a variable identifier.
    :-)

Post a Comment