Forums Register Login
Semantics of "final" keyword - reg.
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!!!
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.
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.
Thanks guys for giving the correct inputs!!!
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?
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.

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();
Thanks Jasper
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 ]
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

All times above are in ranch (not your local) time.
The current ranch time is
Oct 20, 2017 17:43:54.