0% found this document useful (0 votes)
74 views5 pages

Java Static Keyword Guide

The static keyword in Java is used for memory management and allows variables and methods to be accessed without creating an instance of the class. Static variables and methods are associated with the class itself rather than with individual objects. A static variable loads only once during class loading and can be accessed from the class name, saving memory compared to non-static variables which load for each object instance. The main method must be declared static so that it can be called by the JVM without needing to create an object.

Uploaded by

Yasir Imam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views5 pages

Java Static Keyword Guide

The static keyword in Java is used for memory management and allows variables and methods to be accessed without creating an instance of the class. Static variables and methods are associated with the class itself rather than with individual objects. A static variable loads only once during class loading and can be accessed from the class name, saving memory compared to non-static variables which load for each object instance. The main method must be declared static so that it can be called by the JVM without needing to create an object.

Uploaded by

Yasir Imam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Static keyword in java

The static keyword is used in java mainly for memory management. It is used with variables, methods,

blocks and nested class. It is a keyword that are used for share the same variable or method of a given

class. This is used for a constant variable or a method that is the same for every instance of a class. The

main method of a class is generally labeled static.

No object needs to be created to use static variable or call static methods, just put the class name before

the static variable or method to use them. Static method can not call non-static method.

In java language static keyword can be used for following

 variable (also known as class variable)

 method (also known as class method)

 block

 nested class

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

public static final double PI=3.1415;


public static void main(String args[])
{
......
......
}

Difference between static and final keyword

static keyword always fixed the memory that means that will be located only once in the program where

as final keyword always fixed the value that means it makes variable values constant.

Note: As for as real time statement there concern every final variable should be declared the static but there

is no compulsion that every static variable declared as final.


Example of static variable.

In the below example College_Name is always same, and it is declared as static.

Static Keyword Example in Java

class Student
{
int roll_no;
String name;
static String College_Name="ITM";
}
class StaticDemo
{
public static void main(String args[])
{
Student s1=new Student();
s1.roll_no=100;
s1.name="abcd";
System.out.println(s1.roll_no);
System.out.println(s1.name);
System.out.println(Student.College_Name);
Student s2=new Student();
s2.roll_no=200;
s2.name="zyx";
System.out.println(s2.roll_no);
System.out.println(s2.name);
System.out.println(Student.College_Name);
}
}

Example

Output:

100

abcd

ITM
200

zyx

ITM

In the above example College_Name variable is commonly sharable by both S1 and S2 objects.

In the above image static data variable are store in method area and non static variable is store in java

stack. Read more about this in JVM Architecture chapter

Why Main Method Declared Static ?

Because object is not required to call static method if main() is non-static method, then jvm create object

first then call main() method due to that face the problem of extra memory allocation
1. lass Student9{  
2.      int rollno;  
3.      String name;  
4.      static String college = "ITS";  
5.        
6.      static void change(){  
7.      college = "BBDIT";  
8.      }  
9.   
10.      Student9(int r, String n){  
11.      rollno = r;  
12.      name = n;  
13.      }  
14.   
15.      void display (){System.out.println(rollno+" "+name+" "+college);}  
16.   
17.     public static void main(String args[]){  
18.     Student9.change();  
19.   
20.     Student9 s1 = new Student9 (111,"Karan");  
21.     Student9 s2 = new Student9 (222,"Aryan");  
22.     Student9 s3 = new Student9 (333,"Sonoo");  
23.   
24.     s1.display();  
25.     s2.display();  
26.     s3.display();  
27.     }  

28. }  

You might also like