
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
Advertisements