0% found this document useful (0 votes)
32 views5 pages

Final vs Static Final in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

Final vs Static Final in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

final int vs static final int in Java

Key Differences
final int: Constant value per instance (instance-level constant)
static final int: Constant value per class (class-level constant)

Example (final int - Instance Example (static final int - Class


Constants) Constants)
1 public class BankAccount { 1 public class MathConstants {
2 // final int - constant per 2 // static final int - shared
instance by all instances
3 private final int 3 public static final int
accountNumber ; MAX_ITERATIONS = 1000;
4 private final String 4 public static final int
Java Expert Private Constructors in Java November 20, 2025 1/1
Practical Use Cases and Examples

final int - Object-Specific static final int - Shared Constants


Constants 1 public class Configuration {
1 public class Employee { 2 // Application - wide
2 // Each employee has their constants
own 3 public static final int
3 // immutable ID and hire MAX_USERS = 1000;
year 4 public static final int
4 private final int employeeId SESSION_TIMEOUT = 1800;
; 5 public static final int
5 private final int hireYear ; MAX_FILE_SIZE = 10485760; //
6 private String name ; 10 MB
6
7 private double salary ;
8
7 // Status codes
Java Expert Private Constructors in Java November 20, 2025 2/1
Real-World Examples

Example (final int - Immutable Example (static final int -


Object State) Application Constants)
1 public class Transaction { 1 public class HttpStatusCodes {
2 // Each transaction has 2 // HTTP status codes -
immutable ID shared constants
3 private final int 3 public static final int
transactionId ; HTTP_OK = 200;
4 private final long timestamp 4 public static final int
; HTTP_CREATED = 201;
5 private final double amount ; 5 public static final int
6 private String status ; HTTP_BAD_REQUEST = 400;
7 6 public static final int
8 public Transaction ( int id
Java Expert , Constructors in Java
Private HTT P_UNAU THORIZNovember
ED = 20,401;
2025 3/1
When to Use Each

Use final int when: Use static final int when:


Each instance needs its own constant Constant value is shared across all
value instances
Value is determined at object Value is known at compile time
creation Value is the same for all objects
Value differs between instances Examples:
Examples: Mathematical constants
Employee ID Configuration values
Account number Status codes
Transaction ID Application limits
Student roll number Default settings
Order ID
Java Expert Example (static final
Private Constructors in Java int 20, 2025
November 4/1
Summary: final int vs static final int
final int static final int
Instance-level constant Class-level constant
Each object has its own copy Single copy shared by all objects
Memory allocated per instance Memory allocated once per class
Value can differ between instances Same value for all instances
Set in constructor Usually set at declaration
Use for: Use for:
Object-specific identifiers Application-wide constants
Immutable instance properties Mathematical constants
Runtime-determined constants Configuration values
Unique per-object values Shared settings

Key Point
final int: ”This object has a value that won’t change after creation”
static final int: ”All objects
Java Expert ofConstructors
Private this class in Javashare this unchanging
November value”
20, 2025 5/1

You might also like