Static Keyword
Static keyword in java is a non-access modifier. The static keyword makes your program memory efficient. Static keyword is used in java mainly for memory management. Static keyword is used to save memory in java. Static means class level. The static keyword belong to the class than instance of the class.
Static keyword can be apply to variable, method, nested class, block. There is no need to create an object to use to static variable and call static method. Just put the class name before the static variable or static method to use them.
Syntax to declare static variable:
public static void main(String args[])
For example:
Syntax:
Static keyword can be apply to
- Variables
- Methods
- Nested class(A class within another class is called nested class)
- Block
Static keyword cannot be applied to
- If you declare any variable as static, it is known as static variable.
- The static variable can be used to refer the common property of all objects(that is not unique for each objects) e.g collage name of students, company name of employees etc.
- The static variables gets memory only once in class area at the class loading time.
Syntax to declare static variable:
static int a =10;
public static String collage = "IIT";
Employees(int i, String n)
void record()
public static void main(String args[])
Let's understand the problem without static variable
class Employees
{
int emp_id ;
int emp_name ;
String emp_companyname="google";
};
suppose there are 100 employees in the company, now all instance data members gets memory each time when object is created. All employees have it s unique id and name so here instance data member is good but here company name refer to the common property of all objects. If we make it static, this field will get memory only once.
👉: When we want to share common property to each objects than we use static variables.
};
suppose there are 100 employees in the company, now all instance data members gets memory each time when object is created. All employees have it s unique id and name so here instance data member is good but here company name refer to the common property of all objects. If we make it static, this field will get memory only once.
👉: When we want to share common property to each objects than we use static variables.
Example of Static Variable
class Employees
{
int id;
String name;
static String company = "Google";
Employees(int i, String n)
{
id = i;
name = n;
}
void record()
{
System.out.println(id);
System.out.println(name);
System.out.println(company);
OR
System.out.println(id+" "+name +" " +company);
}
public static void main(String args[])
{
Employees e = new Employees(1,"ranvir");
Employees e1 = new Employees(2,"amar");
e.record();
e1.record();
}
}
output: 1 ranvir google
2 amar google
(2) Static Method
- If you apply static keyword with any method, it is known as static method.
- Static method belongs to the class rather than object of a class.
- Static method can be called without creating an object(instance) of class.
- Static methods can access static data member and It can change the value of static data member.
- Any static method can call any other static method in same file.
- You can define many static method. These methods are independent, except that they may refer to each other through calls.
Example of Static Method
Here in this program , we will change the value of static data member through static method and call the static method without creating any object.
class Students
{
int rollno;
String name;
static String college = "IIM";
static void change()
{
college = "MII";
System.out.prinltn("this is static method");
System.out.prinltn("this is static method");
}
Students(int r, String n)
{
rollno = r;
name = n;
}
void record()
{
System.out.println(rollno+""+name+""college);
}
public static void main(String args[])
{
Students.change();//calling static method through the class name
Students s = new Students(10,"bheem");
Students s1 = new Students(11,"karan");
s.record();
s1.record();
}
}
output: this is static method
10 bheem MII
11 karan MII
10 bheem MII
11 karan MII
Restriction for Static method
- The static method cannot access non-static data member and call non-static method directly.
- this and super keyword cannot use in static context.
For example:
class Demo
{
int a = 10;//non-static data member
static void show()//can't access non-static data member
{
System.out.println(a)
}
public static void main(String args[])
{
System.out.println("This is main method");
}
}
output: compile time error
Static Block In java
- Static block is a set of statements and static block will be executed by the JVM before the main method at the time of class loading.
- We can use static block to assign or initialize static data member.
- We can define many static blocks in a class but all these blocks will be executed from top to bottom.
Syntax:
static
{
//Statements;
}
For example:
class StaticBlock
{
static
{
System.out.println("This is static block");
}
public static void main(String args[])
{
System.out.println("this is main method");
}
}
output: This is static block
this is main method
Multiple static block example
class MultipleStaticBlock
{
output: This is static block 1
This is static block 2
this is main method
static
{
System.out.println("This is static block 1");
}
static
{
System.out.println("This is static block 2");
}
public static void main(String args[])
{
System.out.println("this is main method");
}
}
This is static block 2
this is main method
Its really helpful article about Static Keyword in Java. Its helpful for me and my all friends
ReplyDeleteSay, you got a nice article post.Really thank you! Really Great.
ReplyDeletebest online java course
advanced java course