General Java Questions III
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
returns random cases in the range 0..2.
Answer: Don t assume that just because default is first in the code, that it will match
first. In fact the default is only used when there is no matching case, as you have
discovered.
file:///F|/a_jsite/350_tips/general_java III.htm (8 of 9) [2001 07 08 11:24:53]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved