- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Instance variable as final in Java
final is a non-access modifier for Java elements. The final modifier is used for finalizing the implementations of classes, methods, and variables. A final instance variable can be explicitly initialized only once.
A final instance variable should be initialized at one of the following occasions −
At time of declaration.
In constructor.
In instance block.
Compiler will throw error, it a final variable is not initialized at all using any of the above methods. Following examples showcases example of instance variables as final.
Example
public class Tester{ final int A = 1; final int B;{ B = 2; } final int C; Tester(){ C = 3; } public static void main(String[] args) { Tester t = new Tester(); System.out.println("A = " + t.A + ", B = " + t.B + ", C = " + t.C); } }
Output
A = 1, B = 2, C = 3
- Related Articles
- Instance variable in Java
- Final variable in Java
- instance variable hiding in Java
- final local variable in Java
- What is instance variable hiding in Java?
- What is a final variable in java?
- Unreachable statement using final variable in Java
- What is static blank final variable in Java?
- Can we initialize blank final variable in Java
- What is blank final variable? What are Static blank final variables in Java?
- How to use a final or effectively final variable in lambda expression in Java?
- What is a blank uninitialized final variable in java?
- What is blank or uninitialized final variable in Java?
- Unreachable statement using the non-final variable in Java
- Can we declare constructor as final in java?

Advertisements