Java static keyword uses
The primary use of static keyword in java is memory management.Static keyword can be used in
Initialize static data member.
- Variables
- Methods
- blocks
- nested classes.
1.Java static variable
Java static variable gets memory once.This means that the value once assigned does not change.Static variable can be used only to refer a common property with class objects.
Example.
/**
*
* @author Eric
* Blog:www.techoverload.net
*/
public class StudentClass {
int id;
String name;
static String className="BIT2016";
StudentClass(int idNo,String n)
{
id=idNo;
name=n;
}
void showDetails()
{
System.out.println("=========Student Details======");
System.out.println("Student ID "+id);
System.out.println("Student Name "+name);
System.out.println("Student Class"+className);
System.out.println("===============================");
}
public static void main(String args[])
{
StudentClass c=new StudentClass(1112,"Eric");
StudentClass c2=new StudentClass(1113,"Jane");
c.showDetails();
c2.showDetails();
}
}
Counter program without static variable
Below code demonstrate how we loose the value of counter variable because it has not been declared static.This is because the variable get assigned a value each time a new object is created.
Example
/** * * @author Eric * Blog:www.techoverload.net */ public class CounterClass { int count=0; CounterClass() { count++; System.out.println("The count is " +count); } public static void main(String args[]) { CounterClass c=new CounterClass(); CounterClass c2=new CounterClass(); CounterClass c3=new CounterClass(); } }
Output
The count is 1
The count is 1
The count is 1
Counter program with static variable
/** * * @author Eric * Blog:www.techoverload.net */ public class CounterClass { static int count=0; CounterClass() { count++; System.out.println("The count is " +count); } public static void main(String args[]) { CounterClass c=new CounterClass(); CounterClass c2=new CounterClass(); CounterClass c3=new CounterClass(); } }
Output:
The count is 1
The count is 2
The count is 3
2.Java static method
If static keyword is used with any method,it becomes a static method.
Static method xstics
- static method belongs to the class and not the object.
- static method can be called without creating class instance.
- static method can access static data and change them.
Example
Below code shows a static method that has access to static variable and changes it.
/** * * @author Eric * Blog:www.techoverload.net */ public class StudentClass { int id; String name; static String className="BIT2016"; StudentClass(int idNo,String n) { id=idNo; name=n; } static void changeClass() { className="BIT2017"; } void showDetails() { System.out.println("=========Student Details======"); System.out.println("Student ID "+id); System.out.println("Student Name "+name); System.out.println("Student Class"+className); System.out.println("==============================="); } public static void main(String args[]) { StudentClass c=new StudentClass(1112,"Eric"); //calling static method StudentClass.changeClass(); StudentClass c2=new StudentClass(1113,"Jane"); c.showDetails(); c2.showDetails(); } }
Example of static method to do calculations
/** * * @author Eric * Blog:www.techoverload.net */ public class Calculation { static int calc(int p) { return p*p*p; } public static void main(String args[]) { int rs=Calculation.calc(20); System.out.println("The result is "+rs); } }
Note that
- static method cannot use non static data member or call non static method.
- this and super cannot be used in static context.
3.Java static block
The main use of static block is :-
Initialize static data member.
Executed before main method.
public class A2{ static{System.out.println("static block is invoked");} public static void main(String args[]){ System.out.println("Hello main"); } }