This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Ian - This is tricky because you know you don't need an instance to invoke a static method, so what do you do? When you use static methods you have to indicate from which class you're getting the method. In this case try: return Math.sqrt(x); // Math is the class that contains sqrt The rule to remember is: "For static methods, use the dot operator on the class name"
There is another tricky shortcut for similar situations: If you have a static member (variable or method) in one of your classes, (say your class is Dog), and you have an instance of that class (Dog d = new Dog(); ), you can also access static members in the class using the shortcut: d.doStaticMethod(); // using an instance var to access a static method or Dog.doStaticMethod(); The first example is really just a compiler trick, but it works. [ June 04, 2003: Message edited by: Bert Bates ]
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joined: Apr 05, 2001
Thanks, that works fine, need to study up on static methods, do you know any good books?