Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Static Methods and Variables

Variables and methods of the Student class which we have written can be accessed only after creating an object of that class. Write a test program with a main method containing the following line.


You will receive a compilation error saying that a non static method cannot be referenced from a static context. This is what we are going to deal with static and non static variables and methods.

When a variable or a method is not defined used the static keyword, it cannot be accessed by specifying the class name and the dot operator followed by the variable or method name. This is because such items are said to belong to objects which would be instantiated from those classes. Every object would have its own copy of those variables. The class as such doesn't possess that particular variable. On the other hand, when something is defined as static, the particular entity, be it a method or a variable belongs to the class itself. There would be only one copy of that static member and all the objects would be sharing it. These static members can be accessed directly without creating an object. We have use the static method main a number of times in our program. Recall that we have never created the 'HelloWorld' object but still the program had run. This is because the method main() was declared static. We shall now take a deeper look at static variables and static methods.

Static variables

A variable can be declared as static by using the static keyword in the following way.

static int a;

Such variables can be defined only in a class. The keyword static cannot be used for variables that would be declared in methods. Further, a static variable is set to its default value. There is no need to explicitly specify it. However, we are free to initialise it with some other value if we wish to do so.

static int a = 34;

These static members can be accessed through the class as well as through objects created from the class. Further, there would be only one copy of static variables. All objects would be sharing the same copy. The following program illustrates the use of static variables through the use of a class Number which contains a static variable x and a non static variable y.

public class Number {
   static int x; // x will be initialized to its default value 0
   int y; // non static variable

Following is the test class.

public class Test {

   public static void main ( String args[] ) {
      System.out.println ( Number.x );
      Number.x=34; // accessing static variable through class name

      Number num1 =new Number ();
      System.out.println ( num1.x );
      System.out.println( num1.y );
      num1.x = 347; // accessing static variable through an object

      Number num2 = new Number ();
      System.out.println ( num2.x );
      System.out.println( num2.y );

      // The three statements below print the same value as there is only one copy of a static variable which is shared by all the objects
      System.out.println ( Number.x );
      System.out.println ( num1.x );
      System.out.println ( num2.x );

The output would be:


The above program shows that a static variable can be accessed by using the class name, or through an object of that class. Also, there is only one copy of the static variable. That was the reason why the change made through Number.x was reflected on the x that was accessed through num1 also. Further, the static variable was set to zero by default as is evident in the first line of output.

Static methods

Similar to static variables, we have static functions also. Functions or methods are declared as static by specifying the static keyword. A static method can be called either using the class name or the object name. A static function can access only other static members (variables or methods). Attempting to access non static members will result in errors.

Following program shows the usage of static functions.

public class Number {

   static int x = 7;
   int y;

   public static void a () {
      System.out.println ( "static a() ");

   public void b () {
      System.out.println ( "b() ");

   public static void c () {
      System.out.println ( "x is "+x); // static variable a can be accessed
      a() ; // static method a() can be accessed
      y = 34; // non static variable y cannot be accessed in static method
      b(); // non static method b() cannot be accessed in static method

public class Test{

   public static void main(String[] args) {
      Number.c(); // accessing static method using class name
      Number n=new Number();
      n.c(); // accessing static method using object


x is 7
static a()
x is 7
static a()

Invalid code in Number class has been commented out using a multi-line comment. The code contained in that Comment is invalid since an attempt was made to access non static methods and variables from a static method. The Test class shows how static methods can be accessed using both the class name and method name.

Privacy Policy