General Java Questions I
returns an Enumeration will actually be returning an inner class that implements
Enumeration
(and therefore defining hasMoreElements and nextElement in a manner that is
specific to traversing a Hashtable).
If this doesn t help, maybe you could clear up what you mean by Enumeration having
defined methods.
Eric
Q: can anyone provide an example on how to use clone() and clonable interface?
Answer:
//Class Point uses default clone implementation that returns
//shallow copy of Point (which is sufficient in this case).
class Point implements Cloneable {
int x, y;
}
class ImmutableRectangle {
private Point lowerLeft = new Point();
private Point upperRight = new Point();
public Point getLowerLeftPoint() {
try {
return lowerLeft.clone();
}
catch(CloneNotSupportedException e) {
//shouldn t occur since Point is cloneable
throw new InternalError();
}
}
}
we don t want Rectangle points to be changed outside its class. the best
way would be to create a copy of Point object and pass it.
i5mast
MVP for Java 2
http://www.brainbench.com
Q: Why does not switch work properly?
From Thinking In Java, chapter 7, Shapes.java:
// ...
switch((int)(Math.random() * 3)) {
default:
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
}
// ...
How does this switch work??? why is default preceding all other cases?
What I think, this should always return case 0, but still, it properly
file:///F|/a_jsite/350_tips/general_java I.htm (23 of 33) [2001 07 08 11:24:51]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved