[go: up one dir, main page]

0% found this document useful (0 votes)
3 views26 pages

Final and Static Keyword in Java

Uploaded by

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

Final and Static Keyword in Java

Uploaded by

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

UNIT-I

JAVA PROGRAMMING
Dr. Himani Maheshwari
final and static KEYWORD
IN JAVA
DR. HIMANI MAHESHWARI
ASSISTANT PROFESSOR
SOC
FINAL KEYWORD in Java
• The final keyword in java is used to restrict the user. The java final keyword
can be used in many context. Final can be:

1. variable
2. method
3. class
• The final keyword can be applied with the variables, a final variable that have
no value it is called blank final variable or uninitialized final variable.
• It can be initialized in the constructor only.
FINAL KEYWORD in Java
FINAL VARIABLE in Java
• If you make any variable as final, you cannot change the value of final
variable(It will be constant).
class Test{
final int x=90; //final variable
void data(){
x=40;
}
public static void main(String args[]){
Test obj=new Test();
obj.data();
} }
OUTPUT:
error: cannot assign a value to final variable x x=40;
FINAL METHOD in Java
If you make any method as final, you cannot override it.
class Animal{
final void eat(){
System.out.println("Animal is eating...");}
}
class Dog extends Animal{
void eat(){
System.out.println("Dog is eating...");}
public static void main(String args[]){
Dog d= new Dog();
d.eat();
} }
OUTPUT:
error: eat() in Dog cannot override eat() in Animal
FINAL CLASS in Java
If you make any class as final, you cannot extend it.
final class Animal{
void eat(){
System.out.println("Animal is eating...");} }
class Dog extends Animal{
void eatdog() { System.out.println("Dog is eating...");}
public static void main(String args[]){
Dog d= new Dog();
d.eat();
d.eatdog();
} }
OUTPUT:
error: cannot inherit from final Animalclass Dog extends Animal{ ^
What is final parameter in Java?
If you declare any parameter as final, you cannot change the value of it.
class Test{
int square(final int n){
n=n+2;//can't be changed as n is final
n*n;
}
public static void main(String args[]){
Test t=new Test();
t.square(5);
} }
OUTPUT:
error
What is final blank variable in Java?
void display()
A final variable that is not initialized at the time of
declaration is known as blank final variable. {
System.out.println(x);
If you want to create a variable that is initialized at }
the time of creating object and once initialized public static void main(String args[]){
may not be changed, it is useful. For example PAN
CARD number of an employee.
Test obj=new Test();
It can be initialized only in constructor.class Test{ obj.display();
final int x; //blank final variable } }
Test(){ OUTPUT:
x=40; 40
}
Static keyword in Java
• 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 can be:

• Variable (also known as a class variable)


• Method (also known as a class method)
• Block
• Nested (Inner) class
1. 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.
Advantages of static variable
• It makes your program memory efficient (i.e., it saves memory).
static variable
class Student{
int rollno;
String name;
String college=“GRAPHIC ERA";
}
• Suppose there are 500 students in my college, now all instance data members will get
memory each time when the object is created. All students have its unique rollno and
name, so instance data member is good in such case. Here, "college" refers to the
common property of all objects. If we make it static, this field will get the memory
only once.
class Student{
int rollno;
String name;
static String college=“GRAPHIC ERA";
}
static variable
class Student{
int rollno; //instance variable Student s1 = new Student(101,"Karan");
String name; //instance variable Student s2 = new Student(102,"Aman");
static String college =“GRAPHIC ERA"; //static s1.display();
variable s2.display();
Student(int r, String n){ //constructor } }
rollno = r; OUTPUT:
name = n; 101 Karan GRAPHIC ERA
} 102 Aman GRAPHIC ERA
void display (){
System.out.println(rollno+" "+name+" "+college);}
}
class Test{
public static void main(String[] args){
static variable
class Student{
int rollno; //instance variable Student s1 = new Student(101,"Karan");
String name; //instance variable Student s2 = new Student(102,"Aman");
static String college =“GRAPHIC ERA"; //static s1.display();
variable s2.display();
Student(int r, String n){ //constructor } }
rollno = r; OUTPUT:
name = n; 101 Karan GRAPHIC ERA
} 102 Aman GRAPHIC ERA
void display (){
System.out.println(rollno+" "+name+" "+college);}
}
class Test{
public static void main(String[] args){
static variable
Example without static variable
class Test{
OUTPUT:
int count=0;//will get memory each time
1
when the instance is created
1
Test(){
1
count++;//incrementing the value
In this example, we have created an instance
System.out.println(count);
variable named count which is incremented in
}
the constructor. Since instance variable gets
public static void main(String args[]){
the memory at the time of object creation,
//creating objects
each object will have the copy of the instance
Test c1=new Test();
variable. If it is incremented, it won't reflect
Test c2=new Test();
other objects. So each object will have the
Test c3=new Test();
value 1 in the count variable.
} }
static variable
class Test{
OUTPUT:
static int count=0;//will get memory only
1
once and retain its value
2
Test(){
3
count++;//incrementing the value of static
static variable will get the memory only
variable
once, if any object changes the value of the
System.out.println(count);
static variable, it will retain its value.
}
public static void main(String args[]){
//creating objects
Test c1=new Test();
Test c2=new Test();
Test c3=new Test();
} }
static method
If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class, not to the object.
• A static method can be invoked without the need for creating an instance of a
class.
class Calculate{
static int cube(int x){
return x*x*x;
}
public static void main(String args[]){
System.out.println(Calculate.cube(5));
} }
OUTPUT:
125
2. static method
• The static method can not use non static data member or call non-static method
directly.
• A static method can call other static methods and can not call non-static methods.
• A static method cannot refer to “this” or ”super” keyword in anyway.
class Test{
static int x=10;
static void display (){
System.out.println(x);
}
public static void main(String args[]){
Test.display();
} }
OUTPUT:
10
static method
class Test{
static int x=10;
static void display (){
System.out.println(x);
}
public static void main(String args[]){
Test.display();
} }
OUTPUT:
error: non-static variable x cannot be referenced from a static context
System.out.println(x);
^
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.
3. Static block
• Is used to initialize the static data member.
• It is executed before the main method at the time of classloading.
class Test{
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
3. Static block example
// static method
class Main { static void display() {
// static variables System.out.println("a = " + a);
static int a = 23; System.out.println("b = " + b);
static int b; System.out.println("max = " + max);
static int max; }
// static blocks public static void main(String args[]) {
static { // calling the static method
System.out.println("First Static block."); display();
b = a * 4; }}
} OUTPUT:
static { First Static block.
System.out.println("Second Static block."); Second Static block.
max = 30; a = 23
} b = 92
max = 30
4. Nested (Inner) Static class
In Java, we can declare a class inside another class. Such classes are known as nested
classes. Nested classes are of 2 types:
• Static Nested Classes
• Non-static Nested Classes

class OuterClass {
// static nested class
static class NestedClass {...}

// non-static nested class


class InnerClass {...}
}
4. static Nested (Inner) class
}
• A static class is a class that is created inside
}
a class, is called a static nested class in
Java. public static void main(String args[])
{
• It cannot access non-static data members Outerclass.Innerclass obj=new Outerclass.Innerclass();
and methods. obj.msg();
• It can be accessed by outer class name. } }
• It can access static data members of the OUTPUT:
outer class. Value of x is 30
class Outerclass{
static int x=30;
static class Innerclass{
void msg(){
System.out.println("Value of x is "+x);

You might also like