The static keyword in Java is used for memory management and can be applied to variables, methods, blocks, and nested classes. Static variables and methods are associated with the class itself rather than with specific objects. A static variable loads into memory only once during class loading and can be accessed without creating instances of the class. Static methods can access static data members and can be called without creating object instances. Static blocks are used to initialize static variables and are executed during class loading before the main method.
2. static
The static keyword in java is used for memory
management mainly.
The static can be:
variable (also known as class variable)
method (also known as class method)
block
nested class
3. Java static variable
If you declare any variable as static, it is known static variable.
Properties
The static variable can be used to refer the common property
of all objects (that is not unique for each object) e.g.
company name of employees,college name of students etc.
The static variable gets memory only once in class area at the
time of class loading or static variable has a single copy for
the whole class and does not depends on the objects
Advantages
It makes your program memory efficient (i.e it saves
memory).
4. Concept of static variable
Suppose there are 500 students in my college, now all
instance data members will get memory each time when
object is created. All student have its unique rollno and
name so instance data member is good. Here, college
refers to the common property of all objects. If we
make it static, this field will get memory only once.
5. Program Example
class Student8{
int rollno; String name;
static String college ="IIIT NR";
Student8(int r,String n){
rollno = r;
name = n; }
void display ()
{
System.out.println(rollno+" "+name+" "+college);
}
public static void main(String args[]){
Student8 s1 = new Student8(111,"Karan");
Student8 s2 = new Student8(222,"Aryan");
s1.display();
s2.display();
} }
111 Karan IIIT NR
222 Aryan IIIT NR
6. Java static method
If you apply static keyword with any method, it is
known as static method.
Properties
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.
7. Program Example
class Student9{
int rollno;
String name;
static String college = "ITS";
static void change(){
college = "BBDIT";
}
Student9(int r, String n){
rollno = r;
name = n; }
void display (){
System.out.println(rollno+" "+name+" "+college);}
public static void main(String args[]){
Student9.change();
Student9 s1 = new Student9 (111,"Karan");
Student9 s2 = new Student9 (222,"Aryan");
Student9 s3 = new Student9 (333,"Sonoo");
s1.display(); s2.display(); s3.display(); } }
8. output
111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
Restrictions for static method
Two main restriction for static method
The static method can not use non static data member
or call non-static method directly.
this and super keywords cannot be used in static
context.
9. Program Example: output?
class student10{
static int roll;
String name;
static void update()
{
System.out.println(roll);
}
public static void main(String args[])
{
student10.update();
}
}
10. Program Example: output?
class student10{
int roll;
String name;
static void update()
{
System.out.println(roll);
}
public static void main(String args[])
{
student10.update();
}
}
11. Program Example: output?
class student10{
int roll;
String name;
void update()
{
System.out.println(roll);
}
public static void main(String args[])
{
student10.update();
}
}
12. Program Example: output?
public class abc2
{
static int a=10;
static int b=20;
public static void main(String args[]){
int c;
c=a+b;
System.out.println(c);
}
}
13. Exercise
Write a program in java to calculate the factorial of a
number. Declare a method as static to calculate the
factorial of a number. Call the factorial method from
the main function.
14. Java static block
It is used to initialize the static data member.
It is executed before main method at the time of
class loading.
We can also use more than one static block in one
class
16. Static block
A static block helps to initialize the static data members, just
like constructors help to initialize instance members
public class Demo {
static int a;
static int b;
static { a = 10;
b = 20; }
public static void main(String args[])
{
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
} }
17. Static block: Program Example
public class Demo {
int a;
int b;
static { a = 10;
b = 20; }
public static void main(String args[])
{
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
} }