aspose file tools*
The moose likes Beginning Java and the fly likes Semantics of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Semantics of "final" keyword - reg." Watch "Semantics of "final" keyword - reg." New topic
Author

Semantics of "final" keyword - reg.

dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
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

Joined: Dec 13, 2006
Posts: 182
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

Joined: Aug 16, 2005
Posts: 14352
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Thanks guys for giving the correct inputs!!!
dinesh.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
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

Joined: Aug 16, 2005
Posts: 14352
    
  22

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

Joined: Oct 12, 2006
Posts: 134
Thanks Jasper
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
Consider Paul's rocket mass heater.
 
subject: Semantics of "final" keyword - reg.