
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Can a final variable be initialized when an object is created in Java?
Once you declare a variable final, after initializing it, you cannot modify its value further. Moreover, like instance variables, final variables will not be initialized with default values.
Therefore, it is mandatory to initialize final variables once you declare them. If not a compile time error will be generated.
Example
public class FinalExample { final int j; public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.j); } }
Compile time error
FinalExample.java:5: error: non-static variable j cannot be referenced from a static context System.out.println(j); ^ 1 error
Initializing the final variable
You can initialize a final variable in 4 ways −
While declaring it.
public class FinalExample { final int j = 100; public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.j); } }
Output
100
Using the final methods.
import java.util.Scanner; public class FinalExample { final int num = getNum(); public static final int getNum() { System.out.println("Enter the num value"); return new Scanner(System.in).nextInt(); } public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.num); } }
Output
Enter the num value 20 20
Using the Constructors
public class FinalExample { final int num; public FinalExample(int num) { this.num = num; } public static void main(String args[]){ FinalExample obj = new FinalExample(20); System.out.println(obj.num); } }
Output
20
Using the Instance initialization blocks
public class FinalExample { final int num; { num = 500; } public static void main(String args[]){ FinalExample obj = new FinalExample(); System.out.println(obj.num); } }
Output
500
Except in the case of final method, if you initialize the final variable in all the other three ways it will be initialized soon you instantiate its class.
- Related Articles
- Why should a blank final variable be explicitly initialized in all Java constructors?
- What are all the ways an object can be created in Java?
- How many ways a String object can be created in java?
- What is a final variable in java?
- Can we initialize blank final variable in Java
- Final variable in Java
- Can a final class be subclassed in Java?
- Can a constructor be made final in Java?
- When can a .class file get created in Java?
- What is a blank uninitialized final variable in java?
- final local variable in Java
- What is static blank final variable in Java?
- Do all properties of an Immutable Object need to be final in Java?
- How to create a variable that can be set only once but isn't final in Java?
- Instance variable as final in Java

Advertisements