From: denis q. <dqu...@fr...> - 2005-02-28 18:48:29
|
yes, that's why singleton is preferred to static methods. A singleton class can use inheritance. -- Denis > That sounds right. If it's a static method there's no this pointer, so > there would be no way to figure out which class's method to call at > runtime. I don't know the language spec, but since Eclipse gives a > warning whenever a static method is called through an object, I'd > guess that all static methods are statically resolved at compile time. > > > On Mon, 28 Feb 2005 09:25:58 -0800 (PST), Jeff Weber > <jww...@ya...> wrote: > >>--- Hiroo Hayashi <hir...@co...> wrote: >> >>>I can hardly believe what you wrote. If override >>>does not work, the Java >>>environment is useless at all. How did you confirm >>>it? What happen if >>>you put print statement in your >>>calculateChecksum(Patch,int,int,int)? >> >>Defies logic doesn't it? I know I have to keep >>pinching myself. I've tested this numerous times both >>stepping through with a debugger and using print >>statements. Finally, I put together a really simple >>example that demonstrates this (See below). >> >>What I've found out is that it appears to be happening >>because Driver.calculateChecksum(Patch, int, int, int) >>is defined as a static method. >> >>The attached example has a main class (StaticOverride) >>along with two classes, Rectangle, which inherits from >>Object, and Square which inherits from Rectangle. Both >>Rectangle and Draw contain a static draw(int) method >>that prints a message int number of times. Rectangle >>has a non-static draw() method that calls the >>draw(int) method. The main class, StaticOverride >>creates an object of type Square and calls it's draw() >>method. Because Square.draw() is commented out, it >>calls Rectangle.draw() which in turn calls draw(int). >>You would think that because draw(int) is overridden >>in the Square class, that Square.draw(int) would be >>called but nope, Rectangle.draw(int) is called >>instead. >> >>If you make the draw(int) methods non-static in both >>the Rectangle and Square classes, then it works the >>way you'd expect and the Square.draw(int) method is >>called instead. (At least, that's what happens on my >>system). >> >>Go ahead and try it. If you get different results than >>I did, then something's flakey either with my system >>or my brain. >> >>Here's my example: >>---------------------------- >>import java.util.*; >> >>public class StaticOverride { >> >> public static void main (String args[]) { >> Square aSquare = new Square(); >> >> aSquare.draw(); >> } >>} >> >>class Rectangle { >> int nbr = 2; >> >> void draw() { >> draw(nbr); >> } >> >> static void draw (int num) { >> for (int i = 0; i < num; i++) { >> System.out.println("Drawing a >>rectangle."); >> } >> } >>} >> >>class Square extends Rectangle { >> // void draw() { >> // draw(nbr); >> // } >> >> static void draw (int num) { >> for (int i = 0; i < num; i++) { >> System.out.println("Drawing a square."); >> } >> } >>} >> >> >>__________________________________ >>Do you Yahoo!? >>Yahoo! Mail - Easier than ever with enhanced search. Learn more. >>http://info.mail.yahoo.com/mail_250 >> >> >>------------------------------------------------------- >>SF email is sponsored by - The IT Product Guide >>Read honest & candid reviews on hundreds of IT Products from real users. >>Discover which products truly live up to the hype. Start reading now. >>http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click >>_______________________________________________ >>Jsynthlib-devel mailing list >>Jsy...@li... >>https://lists.sourceforge.net/lists/listinfo/jsynthlib-devel >> > > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Jsynthlib-devel mailing list > Jsy...@li... > https://lists.sourceforge.net/lists/listinfo/jsynthlib-devel > > |