- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How many ways are there to initialize a final variable in java?
In Java, final is the access modifier which can be used with a filed, class and a method.
- When a method if final it cannot be overridden.
- When a variable is final its value cannot be modified further.
- When a class is final it cannot be extended.
Initializing a final variable
Once you declare a final variable, it is a must to initialize it. You can initialize the final instance variable −
- At the time of declaration as.
public final String name = "Raju"; public final int age = 20;
- Within an instance (non-static) block.
{ this.name = "Raju"; this.age = 20; }
- Within a default constructor.
public final String name; public final int age; public Student(){ this.name = "Raju"; this.age = 20; }
Note − If you try to initialize the final instance variable elsewhere, a compile-time error will be generated.
Example1: At the time of declaration
In the following Java program, the class Student contains two final variables − name, age and, we are initializing them at the time of declaration −
public class Student { public final String name = "Raju"; public final int age = 20; public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { new Student().display(); } }
Output
Name of the Student: Raju Age of the Student: 20
Example2: within an instance block
In the following Java program, the class Student contains two final variables− name, age and, we are initializing them within an instance block −
public class Student { public final String name; public final int age; { this.name = "Raju"; this.age = 20; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { new Student().display(); } }
Output
Name of the Student: Raju Age of the Student: 20
Example3: within the default constructor
In the following Java program, the class Student contains two final variables − name, age and, we are initializing them within a default constructor −
public class Student { public final String name; public final int age; public Student(){ this.name = "Raju"; this.age = 20; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { new Student().display(); } }
Output
Name of the Student: Raju Age of the Student: 20
- Related Articles
- How many ways are there to initialize the instance variables of a class in java?
- How many ways are there to register a driver in Java?
- Can we initialize blank final variable in Java
- How many ways are there to convert an Array to ArrayList in Java?
- Why we can't initialize static final variable in try/catch block in java?
- Final variable in Java
- How to use a final or effectively final variable in lambda expression in Java?
- 3 ways to initialize an object in Java
- final local variable in Java
- How many ways to iterate a TreeSet in Java?
- How many ways to iterate a LinkedList in Java?
- Are there any ways to Manipulate Strings in Java.
- What is a final variable in java?
- What is blank final variable? What are Static blank final variables in Java?
- How many non-access modifiers are there in Java?
