Working with static methods and variable

Working with static methods and variable


Posted in : Core Java Posted on : October 5, 2010 at 4:23 PM Comments : [ 0 ]

This section describe about static methods and variable.

Working with static methods and variable

When a class member is declared as static, it can be accessed before any object of it's class is created without reference to any object. Static members(methods and variables) are not tied to class instances. Static members can be called without having an instance.

Defining Static Members

You can define  'static' variable as follows :

public static long x;
static public long y;

Static method can be declared as follows :

public class Calculator {
    public static long add(long x, long y) {
        return x + y;
    }
}

Static method and block declaration :

As you know static method can be accessed before any object of it's class is created without reference to any object. You can create static block of code by using 'static' keyword. This is given in following example.

Example :


public class StaticDemo {
	static int x = 3;
	static int y;

	static void display(int w) {
		System.out.println("w = " + w);
		System.out.println("x = " + x);
		System.out.println("y = " + y);
	}

	static {
		System.out.println("Inside Static block");
		y = x * 4;
	}

	public static void main(String args[]) {
		display(21);
	}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac StaticDemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java StaticDemo
Inside Static block
w = 21
x = 3
y = 12

Static block with exception :

You can use exception with static block as follows :

Example :


public class DemoStatic {
	  static Con con;

	  static {
		  System.out.println("Running program explicitly");
	    try {
	      con = new Con();
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }

	  public static void main(String[] argv) {
	    System.out.println(con);
	  }
	}

	class Con {
	  public Con() throws Exception {
	  }

	  public String toString() {
	    return "Con";
	  }
	}

Note: This program will not run but it will show output of "println" statement followed by exceptions .

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac DemoStatic.java

C:\Program Files\Java\jdk1.6.0_18\bin>java DemoStatic
Running program explicitly
java.lang.NoClassDefFoundError: corejava/Con
at corejava.DemoStatic.<clinit>(DemoStatic.java:9)
Caused by: java.lang.ClassNotFoundException: corejava.Con
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Exception in thread "main"

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics