aspose file tools*
The moose likes Beginning Java and the fly likes using sqrt() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using sqrt()" Watch "using sqrt()" New topic
Author

using sqrt()

Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
I am using a little function to return the squre root of an int:
[code]
public double calcSquare(int x)
{
return sqrt(x);

}
[code/]
i get an error,
SquareRoots.java:62: cannot resolve symbol
symbol : method sqrt (int)
location: class SquareRoots
return sqrt(x);
even when i import java.lang.Math;
why?
thanks
Ian
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
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.)
Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
Thanks, that works fine, need to study up on static methods, do you know any good books?
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5

hmmm....
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Thanks, that works fine, need to study up on static methods, do you know any good books?
This one might help.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Ian Cockcroft
Ranch Hand

Joined: Apr 05, 2001
Posts: 46
hahaha, Touch´┐Ż.
Thanks for your help guys.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using sqrt()