Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Static Keyword

Dr. Sapna Malik


Assistant Professor, MSIT
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 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 Static Variable
It makes your program memory efficient (i.e., it saves memory).
Any object can change the value of a instance variable, but
class/static variables can also be manipulated without creating
an instance of the class.
A non static method can access static variable
An object of the class can access static variable.
Example:
Class Student
{
Static String collegeName=“MSIT”;
}
Student.collegeName=“NSIT”;
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.
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.
Static methods are identified to be mostly used when we are
writing any utility methods like math library
Why is the Java main method 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.
Example
class Demo{
public static int a = 100;
public int b =2 ;
public void showAB(){
System.out.println("A= "+a + "B= "+b);
}
public static void showA(){
System.out.println("A= "+a);
// System.out.println(“B= “+b); error
} Output: A=35
} A=-300,B=200
class Demotry {
public static void main(String[] args){
Demo.a = 35;
//Demo.b=22; // ERROR this is not valid for non static variable
Demo d = new Demo();
d.b = 200; // valid to set a value for a non static variable after creating an instance.
Demo.showA(); //prints 35
d.a=300;
d.showAB();
}
}
Example of static method
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);  
  }  
}  
A static block
The static block, is a block of statement inside a Java class that
will be executed when a class is first loaded in to the JVM.
It is preceded by keyword Static and executed only once.
A static block helps to initialize the static data members, just
like constructors help to initialize instance members.
Syntax
Static
{
//java codes
}
A class may have any number of static block.
Static blocks are executed before the main() method.

10
A static block-Advantages
If the driver or other items need to be loaded into the
namespace ,then static block may be used.
If computation is needed to be done to initialize the static
block can be declared which gets exactly once ,when the
class is first loaded.
It handles security related issues or logging related tags.

11
A static block-Limitation
JVM has a limitation that static block should not exceed
64k.
Checked exception can not be thrown in static blocks.
Exception can be handled with try catch block for static
block.
This and super keywords can not be used I static blocks
Static block can not be declared inside method.

12
A static block-Example 1
class Demotry {
static{
System.out.println("i am in static block");
int a=2,b=3,c=0;
c=a+b;
System.out.println("c= "+c);
}
Demotry(){
System.out.println("i am in constructor ");
}
public static void main(String[] args){
Demotry d = new Demo();
}
static {
System.out.println("i am in second static block");
}
}
Output:
i am in static block
C=5
i am in second static block
i am in constructor

13
A non static block/Constructor block
Non Static block/constructor block are the normal block
without any preceding.
Syntax
{
//Java Codes
}
Non Static block is executed for each object that is
created.
It can initialize instance variable in a class.
Non static blocks will be copied into each constructor of
the class.
14
A non static block/Constructor block
Example
class Demotry1 {
{
System.out.println("i am in non static block/constructor block");
}
Demotry1(){
Output:
System.out.println("i am in constructor"); i am in static block
} i am in non static
Demotry1(int d){ block/constructor block
System.out.println("i am in parameterized constructor");
i am in second non static
block/constructor block
}
i am in constructor
public static void main(String[] args){ i am in non static
Demotry1 c=new Demotry1(); block/constructor block
Demotry1 d=new Demotry1(5); i am in second non static
block/constructor block
}
i am in parameterized constructor
static{
System.out.println("i am in static block");
}
{
System.out.println("i am in second non static block/constructor block");
}
15 }
Method Overloading
If a class has multiple methods having same name but
different in parameters, it is known as Method
Overloading.
Method overloading increases the readability of the
program.
There are two ways to overload the method in java
o By changing number of arguments
o By changing the data type.
Method Overloading is not possible by changing the
return type of the method only.

16

You might also like