Z
Zerex71
Lew said:Don't have instance variables for x, y and z. Define the methods in terms of
the underlying, inherited array.
public double x()
{
return array [0] [0];
}Zerex71 said:Your last suggestion I can live with. I just wanted to use the x, y,
and z names as ways to reference.
That still leaves the question of how the array gets updated if I am
using the class in a static fashion
i.e. not setting its individual members to particular values.
Use an appropriate setBlah( double val ) method.
Or perhaps setBlah( double val, int row, int col ).
This business of hiding all attributes behind accessor methods is quite
powerful, at the cost of verbosity. A method pair "setX()" and "getX()" need
never reference an actual instance variable "x" within the black box. They
only need to present to API clients the view of "x" as an attribute.
Don't use static members unless you really intend the value to be what it is
for the entire class. If your analytical model says, "I can have two Vectors,
call them 'va' and 'vb', and they can contain separate values, and not be
equal in value to each other", then the model requires that the "values" for
Vectors be instance-level.
While you're at it, you will want to override the Object methods equals() and
hashCode(), in a consistent manner.
You also face the decision of whether to make Matrix instances immutable, in
effect "read-only" after construction. There are distinct benefits to doing
so, but it complicates objects that support calculations.
Joshua Bloch covers these overrides and immutability admirably in his book
/Effective Java/, and there are many articles on these topics. GIYF. So are
IBM DeveloperWorks and Sun's Java sites.
P.S., in UMLish diagrams and class design documentation, I model accessor and
mutator methods as attributes, not methods. At times some of my colleagues
want to change the design docs to match the physical implementation as
methods. This is, of course, working the wrong way around.
- Lew
Hi Lew,
The thing with using the static functionality is that it's an area of
use that I want to investigate.
Not only am I developing an application but I'm also expanding my
thoughtview of the language. To that
end, I am learning how best to take advantage of static vs. non-
static. In other words, I often have
the need for a class to do some utility on behalf of some other code,
but not necessarily to have
an instance, and yet, the class can bind up instance-based methods and
attributes in one place (if any
of this makes any sense).
In other words, I want Vector to represent individual instances of
unchangeable values (I don't modify a vector
but will instead, operate on it to produce another result/instance),
but I also want Vector to have operations
that can be used without requiring an instance (although in pure terms
this is not *that* likely to happen --
I won't usually ask for the unit vector without having an actual
vector to perform that operation on).
Thanks for writing.