
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
Blank Final in Java
In Java, a final variable can a be assigned only once. It can be assigned during declaration or at a later stage. A final variable if not assigned any value is treated as a blank final variable. Following are the rules governing initialization of a blank final variable.
A blank instance level final variable cannot be left uninitialized.
The blank Instance level final variable must be initialized in each constructor.
The blank Instance level final variable cannot be initialized in class methods.
A blank static final variable cannot be left uninitialized.
The static final variable must be initialized in a static block.
A static final variable cannot be initialized in constructor or class methods.
Example
public class Tester { public final int a; public static final int b; static { b = 2; } Tester() { this(1); } Tester( int a) { this.a = a; } public static void main(String[] args) { Tester tester = new Tester(); System.out.println("a = " + tester.a + ", b = " + b); } }
Output
a = 1, b = 2
Advertisements