Java Static Keyword

  1. Create a Simple Static method with a Constructor in Java
  2. Differentiate Static method with a normal method in Java
  3. Create Multiple Static methods in Java
  4. Create Static variables in Java
  5. Static Variable vs Ordinary Variable in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Create a Simple Static method with a Constructor in Java


Static_Ex1.java

public class Static_Ex1 {
 
	static {
		
		System.out.println("The Static");
	}
	
	Static_Ex1() {
		
		System.out.println("The Constructor");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		new Static_Ex1();
	}
}

Sample Output

The Static
The Constructor




B. Differentiate Static method with a normal method in Java


Static_Ex2.java

public class Static_Ex2 {
 
	static {
		
		System.out.println("The Static");
	}
	
	Static_Ex2() {
		
		System.out.println("The Constructor");
	}
	
	void Method() {
		
		System.out.println("The Method");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Static_Ex2 obj = new Static_Ex2();
		
		obj.Method();
	}
}

Sample Output

The Static
The Constructor
The Method




C. Create Multiple Static methods in Java


Static_Ex3.java

public class Static_Ex3 {
 
	static {
		
		System.out.println("The Static");
	}
	
	Static_Ex3() {
		
		System.out.println("The Constructor");
	}
	
	void Method() {
		
		System.out.println("The Method");
	}
}
 
class MainClass {
	
	static {
		
		System.out.println("The MainClass Static");
	}
	
	public static void main(String args[]) {
		
		Static_Ex3 obj = new Static_Ex3();
		
		obj.Method();
	}
}

Sample Output

The MainClass Static
The Static
The Constructor
The Method




D. Create Static variables in Java


Static_Ex4.java

public class Static_Ex4 {
 
	static int a = 10;
	static float b = (float) 20.63;
	static double c = 123.4567890123;
	static boolean d = true;
	static char e = 'A';
	static String f = "SampleCodez";
	
	Static_Ex4() {
		
		System.out.println("The Static Variables are : \n");
		System.out.println("The int value is : " + a);
		System.out.println("The float value is : " + b);
		System.out.println("The double value is : " + c);
		System.out.println("The boolean value is : " + d);
		System.out.println("The char value is : " + e);
		System.out.println("The String value is : " + f);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		new Static_Ex4();
	}
}

Sample Output

The Static Variables are : 

The int value is : 10
The float value is : 20.63
The double value is : 123.4567890123
The boolean value is : true
The char value is : A
The String value is : SampleCodez




E. Static Variable vs Ordinary Variable in Java


Static_Ex5.java

public class Static_Ex5 {
	
	int i = 5;
	static int j = 5;
		
	void display() {
		
		System.out.println("int Variable : " + i);
		System.out.println("Static int Variable : " + j);
		
		i = i + 10;
		j = j + 10;
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Static_Ex5 obj1 = new Static_Ex5();
		Static_Ex5 obj2 = new Static_Ex5();
		
		obj1.display();
		obj2.display();
	}
}

Sample Output

int Variable : 5
Static int Variable : 5
int Variable : 5
Static int Variable : 15






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com