• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

final variable related problem.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from K&B 3rd chapter, 8th question.
What will be the output on the line,System.out.println();
The correct answer is A.
I am not getting the explanation given in K&B.
The explanation is as "The references f1, z, and f3 all refer to the same instance of Fizz. The final
modifier assures that a reference variable cannot be referred to a different object, but final
doesn�t keep the object�s state from changing."


class Fizz {
int x = 5;
public static void main(String[] args) {
final Fizz f1 = new Fizz();
Fizz f2 = new Fizz();
Fizz f3 = FizzSwitch(f1,f2);
System.out.println((f1 == f3) + " " + (f1.x == f3.x));
}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z = x;
z.x = 6;
return z;
} }
What is the result?
A. true true
B. false true
C. true false
D. false false
E. Compilation fails.
F. An exception is thrown at runtime.

Regards,
Hamraj
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by hamraj happy:
... I am not getting the explanation given in K&B...


What part of the explanation are you not getting?

If you tell us your reasoning about how you expected this code to work, then we can probably help you better.
 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

I am explaining the things what i am understanding with this code,so i hope you will able to understand my problem.

In method(Fizz x, Fizz y) f1, f2 are passed.
Since f1 is marked as final,so we can't change the
value of f1.
but in method, method(Fizz x, Fizz y)

f1 is passed to x, so final Fizz z = x means final Fizz z = f1.

So z.x = 6 shouldn't be changed as it is assigned as final.
Why the code is not giving the compile time error.Since final variable can't be changed.

One thing more which i am doubtful is how final object works, because here its not a final variable its a final object(final Fizz f1 = new Fizz();)?
Because i know about final variable, final method and final class.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by hamraj happy:
... So z.x = 6 shouldn't be changed as it is assigned as final...

One thing more which i am doubtful is how final object works, because here its not a final variable its a final object(final Fizz f1 = new Fizz();)? ...


As you noted, "final" can be applied to classes, methods, and variables. But not objects. (Objects might be immutable, but that's different.)

f1 and z are both final reference variables, meaning that once they have been assigned a reference, that reference cannot be changed. However, the object referenced by these variables has an int variable x, and x is not a final variable.

In particular, z.x represents a non-final int value. The final variable z cannot be changed to reference anything else, but z.x can be changed to a different value.
 
Hamraj Kulshreshtha
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Marc.

I understood this very well.

Thanks once again.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic