0% found this document useful (0 votes)
2 views6 pages

04_Instance Initializer Block and Final Keyword in Java

The document explains the Instance Initializer Block in Java, which initializes instance data members when an object is created, and outlines the rules governing its use. It also details the final keyword, which restricts modifications to variables, methods, and classes, including examples of final variables, methods, and classes. Additionally, it covers blank final variables, static blank final variables, and final parameters, concluding with the note that constructors cannot be declared as final.

Uploaded by

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

04_Instance Initializer Block and Final Keyword in Java

The document explains the Instance Initializer Block in Java, which initializes instance data members when an object is created, and outlines the rules governing its use. It also details the final keyword, which restricts modifications to variables, methods, and classes, including examples of final variables, methods, and classes. Additionally, it covers blank final variables, static blank final variables, and final parameters, concluding with the note that constructors cannot be declared as final.

Uploaded by

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

JAVA OOP

Instance Initializer Block in Java


Instance Initializer block is used to initialize the instance data member. It run each
time when object of the class is created.
1. class Bike7{
2. int speed;
3.
4. Bike7(){System.out.println("speed is "+speed);}
5.
6. {speed=100;}
7.
8. public static void main(String args[]){
9. Bike7 b1=new Bike7();
10. Bike7 b2=new Bike7();
11. }
12. }
13.
Output:speed is 100
speed is 100

Rules for instance initializer block :


1. The instance initializer block is created when instance of the class is
created.
2. The instance initializer block is invoked after the parent class constructor is
invoked (i.e. after super() constructor call).
3. The instance initializer block comes in the order in which they appear.

final keyword in Java


The final keyword in Java is used to restrict the user. It is also known as a non-
access modifier. We can use the final keyword with:
1. Variable
2. Method
3. Class

Page 1 of 6
JAVA OOP

4. Parameter

1) Java final variable


When a variable is declared as final, it is known as a final variable. Its value cannot
be changed once initialized. It behaves like a constant.
Syntax:

1. final datatype VARIABLE_NAME=VALUE;

Example:
1. class Main {
2. final int SPEED_LIMIT=90; //final variable
3. void run() {
4. SPEED_LIMIT=400; //we cannot change the final variable
5. }
6. public static void main(String args[]) {
7. Main obj=new Main();
8. obj.run();
9. }
10. }

When we compile the above code, it shows a compile-time error, as follows:


error: cannot assign a value to final variable SPEED_LIMIT

2) Java final Method


A method declared as final is known as a final method. Subclasses cannot override
the final method. But the final method can be inherited.
Syntax:

1. final void paint() {


2. }
Example:
1. class Bike {
2. final void run() { //final method

Page 2 of 6
JAVA OOP

3. System.out.println("running"); }
4. }
5. public class Main extends Bike {
6. //We cannot override the final method
7. void run(){System.out.println("running safely with 100kmph");
8. }
9. public static void main(String args[]) {
10. Main obj= new Main();
11. obj.run();
12. }
13. }

When we compile the above code, it shows a compile-time error, as follows:


Main.java:7: error: run() in Main cannot override run() in Bike
void run(){System.out.println("running safely with 100kmph");
^
overridden method is final
1 error

Can we inherit final method?


Ans) Yes, the final method can be inherited, but we cannot override it. For
Example:
1. class Bike {
2. final void run() { System.out.println("running..."); }
3. }
4. public class Main extends Bike {
5. public static void main(String args[]) {
6. new Main().run();
7. }
8. }

3) Java final Class


A class declared with the final keyword is known as a final class. Note that the
final class cannot be inherited.
Syntax:

1. final class Square {


2. //statement
Page 3 of 6
JAVA OOP

3. }

Example:
1. final class Bike {}
2. //We cannot inherit the final class
3. public class Main extends Bike
4. {
5. void run()
6. {
7. System.out.println("running safely with 100kmph");
8. }
9. public static void main(String args[])
10. {
11. Main obj = new Main();
12. obj.run();
13. }
14. }
When we compile the above code, it shows a compile-time error, as follows:
Main.java:3: error: cannot inherit from final Bike
public class Main extends Bike
^
1 error

Blank or uninitialized final variable:


A final variable that is not initialized at the time of declaration is known as a blank
final variable. It can be initialized only in a constructor.
1. class Student {
2. int id;
3. String name;
4. final String PAN_CARD_NUMBER;
5. ...
6. }
Initialize:
1. public class Main {
2. final int speedlimit; //blank final variable
3. Main() {
4. speedlimit=70;
5. System.out.println(speedlimit);
6. }
7. public static void main(String args[]) {
Page 4 of 6
JAVA OOP

8. new Main();
9. }
10. }

Output:

70
static blank final variable:
A static final variable that is not initialized at the time of declaration is known as a
static blank final variable. It can be initialized only in a static block.

Example of Static Blank Final Variable


1. public class Main {
2. static final int data;//static blank final variable
3. static{ data=50;}
4. public static void main(String args[]) {
5. System.out.println(Main.data);
6. }
7. }

Output:

50

final parameter:
If you declare any parameter as final, we cannot change its value of.
1. public class Main {
2. public void value(final int num){
3. System.out.println(num);
4. }
5. public static void main(String args[]) throws Exception{
6. Main obj = new Main();
7. obj.value(500);
8. }
9. }

Page 5 of 6
JAVA OOP

Output:

500

Can we declare a constructor final?


Ans) No, because a constructor never inherited.

Page 6 of 6

You might also like