0% found this document useful (0 votes)
2 views

Static

The document explains the use of the 'static' keyword in Java, which is primarily for memory management and can be applied to variables, methods, blocks, and nested classes. It details the behavior of static variables and methods, including their shared nature among instances and restrictions on their usage. Additionally, it discusses the significance of the main method being static and the role of static blocks in class initialization, noting that as of JDK 1.7, a Java program cannot run without a main method.

Uploaded by

xandercageyo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Static

The document explains the use of the 'static' keyword in Java, which is primarily for memory management and can be applied to variables, methods, blocks, and nested classes. It details the behavior of static variables and methods, including their shared nature among instances and restrictions on their usage. Additionally, it discusses the significance of the main method being static and the role of static blocks in class initialization, noting that as of JDK 1.7, a Java program cannot run without a main method.

Uploaded by

xandercageyo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Java “static” Keyword

Brajesh Raj
Index
Static variable
Program of the counter without static variable
Program of the counter with static variable
Static method
Restrictions for the static method
Why is the main method static?
Static block
Can we execute a program without main
method?
Introduction
The static keyword in Java is used for memory
management mainly. We can apply static keyword
with variables, methods, blocks and nested
classes. The static keyword belongs to the class
than an instance of the class.

The static can be:

◦ Variable (also known as a class variable)


◦ Method (also known as a class method)
◦ Block
◦ Nested class
Java static variable
If you declare any variable as static, it is
known as a static variable.
◦ The static variable can be used to refer to the
common property of all objects (which is not unique
for each object), for example, the company name of
employees, college name of students, etc.
◦ The static variable gets memory only once in the
class area at the time of class loading.
◦ Java creates only one copy of static variable which
can be used even if class is not instantiated
Program of the counter without
static variable
In this example, we have created an instance
variable named count which is incremented in
the constructor. Since instance variable gets
the memory at the time of object creation,
each object will have the copy of the instance
variable. If it is incremented, it won't reflect
other objects. So each object will have the
value 1 in the count variable.
E:\Java\Static\Counter.java
//Java Program to demonstrate the use of an instance variable
//which get memory each time when we create an object of the class.
class Counter{
int count=0;//will get memory each time when the instance is created

Counter(){
count++;//incrementing value
System.out.println(count);
}

public static void main(String args[]){


//Creating objects
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:
1
1
1
Program of counter by static
variable
//Java Program to illustrate the use of static variable which
//is shared with all objects.
class Counter2{
static int count=0;//will get memory only once and retain its value

Counter2(){
count++;//incrementing the value of static variable
System.out.println(count);
}

public static void main(String args[]){


//creating objects
Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();
}
}
E:\Java\Static\Counter2.java
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 the
object of a class.
◦ A static method can be invoked without the need
for creating an instance of a class.
◦ A static method can access static data member and
can change the value of it.
◦ E:\Java\Static\TestStaticMethod.java
//Java Program to demonstrate the use of a static method.
class Student{
int rollno;
String name;
static String college = "ITS";
//static method to change the value of static variable
static void change(){
college = "BBDIT";
}
//constructor to initialize the variable
Student(int r, String n){
rollno = r;
name = n;
}
//method to display values
void display(){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to create and display the values of object
public class TestStaticMethod{
public static void main(String args[]){
Student.change();//calling change method
//creating objects
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method
s1.display();
s2.display();
s3.display();
}
}
Restrictions for the 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.
class A{
int a=40;//non static

public static void main(String args[]){


System.out.println(a);
}
}
//Compile time error
A.java
Why the main() method is
static?
It is because the object is not required to call
a static method. If it were a non-static
method, JVM creates an object first then call
main() method that will lead the problem of
extra memory allocation.
Java static block
Is used to initialize the static data member.
It is executed before the main method at the
time of classloading.
class A2{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Output:static block is invoked
Hello main
A2.java
Can we execute a program without
main() method?
No, one of the ways was the static block, but
it was possible till JDK 1.6. Since JDK 1.7, it is
not possible to execute a Java class without
the main method.
class A3{
static{
System.out.println("static block is invoked");
System.exit(0);
}
}

A3.java
THANK YOU

You might also like