Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Semantics of "final" keyword - reg.

 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

This is a very basic question.

Please look at the snippet:

final Car myCar = new Car();



This creates the instance myCar and i want it to be constant but still i am able to modify the states of the instance myCar through its mutator method.

One solution is making all the attributes of the class Car as "final". But then the entire class itself will become Immutable.

Is there any other way. Or Am I wrong in perceiving the concept of final.


Thanks in Advance!!!
dinesh
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by defining a variable as final, you can't change the reference variable, but you make the method calls that may modify the internal
state of the variable.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ajay already says, "final" does not make the object that the variable refers to unmodifiable. It just makes the variable itself unmodifiable, i.e. you can't change the value of the variable - but you can change the state of the object that the variable refers to.

There is no keyword in Java to do what you want to do.
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for giving the correct inputs!!!
dinesh.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to make clear that the Car instance shouldn't be mutated is introducing an interface that only contains the operations that don't mutate the car:

ReadonlyCar myCar = new Car();

or perhaps even better

Car myCar = new MutableCar();

How does that sound to you?
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja,

Thanks for invoking such wonderful idea.

Your Theme sounds Excellent but could you please give me some more inputs on this?

I understand your idea like this:

1) Creating an interface "Car" that contains no mutators.

2) Then creating two concrete implementations such as "MutableCar" and "ImmutableCar" Am i Correct?

3) If so then how can i have the attributes?

This may be very silly question. But since i am not familiar with Design Pattern kind of stuffs, i need your help.

thanks,
dinesh.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15438
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you have to do it the other way around:

1) Create an interface ReadonlyCar (or ImmutableCar, whatever you want to call it).

2) Write a class Car that implements ReadonlyCar, and also has some extra methods that do allow you to modify the car.

Then use what Ilja proposes above when you need a read-only or a read-write Car:

// Car with read-only access
ReadonlyCar myCar1 = new Car();

// Car with read-write access
Car myCar2 = new Car();
 
dinesh Venkatesan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jasper
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
Write a class Car that implements ReadonlyCar, and also has some extra methods that do allow you to modify the car.


You're proposing a mutable class which implements an interface with "Readonly" in its name. That doesn't sound very nice. Wherever possible the English names and ordinary meanings of classes should match their meanings in the program.

Sometimes, as code develops, one can't avoid some degradation of the natural meaning of class names, but one shouldn't design such confusion into the initial version.

What about "Readable" instead of "Readonly". If something is readable, that doesn't say anything about whether it can or cannot be written.
[ January 16, 2007: Message edited by: Peter Chase ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic