Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on final

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B, onf inal, it is given

Final references variables cannot refer to different object once the object has assigned to the final variable.. The data can be modified ..
.
Ranchers, can you please expalin me with example and clear my doubt..
Please ...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To continue from yesterday's thread....
Why don't you write one for us that tests this?

Have it demonstrate what will happen if you try to reassign a final variable.
Show us how you can change the data contained by the object to which the variable is assigned (for example: change the value of a StringBuilder object).

Tell us what you expected the program to do.
Tell us what happened when you tried to compile it in both cases.
Tell us what happened when you ran this program.
[ April 09, 2008: Message edited by: Ben Souther ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Fiz
{
int x=5;

public statci void main(String s[])
{
final Fiz f1 = new Fiz();
Fiz f2 = new Fiz();
Fiz f3 = Fizswitch(f1,f2);
if(f1==f2) f1=f2 ;

System.out.println(f1.x = f3.x);
}

static Fiz Fizswitch(Fiz x, Fiz y)
{
final Fiz z =x;
z.x =6;
return z;
}

o/p :Compilation fails
Can you now expalin me, why it failed.
i think f2 an f1 blong both to Fiz object.

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and...
what error message did the compiler give you when it failed?
(I'm assuming that you got past the typos like 'statci' and the missing curly brace at the end.)
[ April 09, 2008: Message edited by: Ben Souther ]
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dinesh,

Despite what Ben said above, your code doesn't compile because you try to assign final f1 to another object which is referred by f2 in line if (f1==f2) f1=f2. This line will give output compiler error.

thanks & regards
-Mila-
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Fiz
{
int x=5;

public static void main(String s[])
{
final Fiz f1 = new Fiz();
Fiz f2 = new Fiz();
Fiz f3 = Fizswitch(f1,f2);
//if(f1==f2) f1=f2 ;
System.out.println(f1.x = f3.x);
}

static Fiz Fizswitch(Fiz x, Fiz y)
{
final Fiz z =x;
z.x =6;
return z;
}

}

o/p-6.

Can you explain me please...?
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In K&B, onf inal, it is given

quote:Final references variables cannot refer to different object once the object has assigned to the final variable.. The data can be modified ..


Hi Dinesh,

the answer of your question is from explanation that you have quoted above. it gives output 6 because you modified data/state from method Fizswitch. The original value is 5 and after modified it gives you 6.
hope this help.

thanks & regards,
-mila-
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to understand that setting a reference variable to final, it doesnot mean that the object and the variables defined in it are set to final.

This can be proved by the examples that you specified.

The first one
you are trying to assign the reference variable to another object which is not possible because you set the reference variable to final.

In the second case
you are updating the variable of the object of a reference variable that is final. This is perfectly legal as the variable is not set to final.

hope this helps

thanks
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means that i have not assign intx as final int x.
If i assign it as final intx then i can't change the value.
Correct me if iam wrong
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya thats the whole point. You cannot change the value that you declare as final. I can either be reference variable or an instance variable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic