*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on final" Watch "Question on final" New topic
Author

Question on final

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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 ...


Thanks<br />Dinesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Feb 25, 2008
Posts: 61
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-


SCJP 5
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Feb 25, 2008
Posts: 61

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

Joined: Feb 10, 2008
Posts: 179

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


-kolli
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Feb 10, 2008
Posts: 179

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on final