-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
moshadab
committed
Sep 17, 2022
1 parent
8d2deec
commit 27c56aa
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/com/shadab/java/core/accessmodifiers/FinalVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.shadab.java.core.accessmodifiers; | ||
|
||
public class FinalVariable { | ||
|
||
public static final int rank =20; | ||
public static final Data data =new Data(); | ||
|
||
} | ||
|
||
class FinalTest{ | ||
|
||
public static void main(String[] args) { | ||
FinalVariable finalVariable = new FinalVariable(); | ||
// finalVariable.rank=30; //--> The final field FinalVariable.rank cannot be assigned | ||
System.out.println(finalVariable.rank); | ||
//FinalVariable.data= new Data(); //--> Final variable cant be rebound to another refrence | ||
System.out.println("id:"+FinalVariable.data.id+"name:"+FinalVariable.data.name); | ||
Data data =FinalVariable.data; | ||
// but internal of the object refenrenced by final variable can be modified | ||
data.id=23; | ||
data.name="Rohan"; | ||
System.out.println("id:"+FinalVariable.data.id+"name:"+FinalVariable.data.name); | ||
} | ||
|
||
} | ||
|
||
class Data{ | ||
int id ; | ||
String name; | ||
} |