You are on page 1of 10

Java static keyword

The static keyword in java is used for memory management mainly.


We can apply java static keyword with variables, methods, blocks and
nested class. The static keyword belongs to the class than instance of
the class.

In java language static keyword can be used for following

1. variable (also known as class variable)


2. method (also known as class method)
3. block
4. nested class

1) Static variable

If any variable we declared as static is known as static variable.


 Static variable is used for fulfill the common requirement. For
Example company name of employees,college name of students
etc. Name of the college is common for all students.
 The static variable allocate memory only once in class area at the
time of class loading.

Advantage of static variable

Using static variable we make our program memory efficient (i.e it


saves memory).
When and why we use static variable

Suppose we want to store record of all employee of any company, in


this case employee id is unique for every employee but company name
is common for all. When we create a static variable as a company name
then only once memory is allocated otherwise it allocate a memory
space each time for every employee.
Syntax for declare static variable:

public static variableName;

Syntax for declare static method:

public static void methodName()


{
.......
.......
}

Syntax for access static methods and static variable

Syntax

className.variableName=10;
className.methodName();

Example of static variable

//Program of static variable

class Student
{
int rollno;
String name;
static String institute ="IANT";

Student(int r,String n)
{
rollno = r;
name = n;
}
void display ()
{
System.out.println(rollno+" "+name+" "+ institute);
}

public static void main(String args[])


{
Student s1 = new Student(101,"Ravi");
Student s2 = new Student(202,"Priya");

s1.display();
s2.display();
}
}

Program of counter without static variable


class Counter
{
int count=0;//will get memory when instance is created
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}

Program of counter by static variable

class Counter
{
static int count=0;//will get memory only once and retain its value
Counter()
{
count++;
System.out.println(count);
}

public static void main(String args[])


{
Counter c1=new Counter2();
Counter c2=new Counter2();
Counter c3=new Counter2();
}
}
Difference between non-static and static variable

Non-static variable Static variable


1)These variable should not be 1)These variables are preceded by
preceded by any static keyword static keyword
2)Memory is allocated for these 2)Memory is allocated for these
variable whenever an object is variable at the time of loading of
created the class.
3)Memory is allocated multiple 3)Memory is allocated for these
time whenever a new object is variable only once in the program.
created.
4)Non-static variable also known 4)Memory is allocated at the time
as instance variable while because of loading of class so that these
memory is allocated whenever are also known as class variable.
instance is created.
5)Non-static variable are specific 5)Static variable are common for
to an object every object that means there
memory location can be sharable
by every object reference or same
class.
6)Non-static variable can access 6)Static variable can access with
with object reference. class reference.
Syntax Syntax

obj_ref.variable_name class_name.variable_name
2) Java static method

If you apply static keyword with any method, it is known as static


method.
 A static method belongs to the class rather than object of a class.
 A static method can be invoked without the need for creating an
instance of a class.
 static method can access static data member and can change the
value of it.

Java static method vs instance method


class Difference
{
public static void main(String[] args)
{
display(); //calling without object
Difference t = new Difference();
t.show(); //calling using object
}

static void display()


{
System.out.println("Programming is amazing.");
}

void show()
{
System.out.println("Java is awesome.");
}
}

//Program of changing the common property of all


objects(static field).

class Student
{
int rollno;
String name;
static String school = "SP";

static void change()


{
college = "Shree";
}

Student(int r, String n)
{
rollno = r;
name = n;
}

void display ()
{
System.out.println(rollno+" "+name+" "+school);
}
public static void main(String args[])
{
Student.change();

Student s1 = new Student (102,"Priya");


Student s2 = new Student (202,"Manoj");
Student s3 = new Student (303,"Suraj");

s1.display();
s2.display();
s3.display();
}
}

Another example of static method that performs normal


calculation
class Calculate
{
static int cube(int x)
{
return x*x*x;
}

public static void main(String args[])


{
int result=Calculate.cube(5);
System.out.println(result);
}
}
Restrictions for static method

There are two main restrictions for the static method.


They are:
 The static method can not use non static data member or call
non-static method directly.
 this and super cannot be used in static context.

Question:why java main method is static?


 because object is not required to call static method if it were non-
static method, jvm create object first then call main() method that will
lead the problem of extra memory allocation.

3) Java static block

 Is used to initialize the static data member.


 It is executed before main method at the time of classloading.

Example of static block


class A
{
static
{
System.out.println("static block is invoked");
}
public static void main(String args[])
{
System.out.println("Hello main");
}
}

Question: Can we execute a program without main() method?

Yes, one of the way is static block but in previous version of JDK not
in JDK 1.7.

class A
{
Static
{
System.out.println("static block is invoked");
System.exit(0);
}
}

You might also like