Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abs()

 
neeraj
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the error' Method abs() not found' error . when I compile the following code.
Please explain me why???
import java.lang.Math;
public class Testexam {
public static void main (String args []) {
double d = 12.56;
System.out.println(abs(d));
}
}

 
Thandapani Saravanan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you must tell the compiler which class the method belongs to. Here it is Math and Math.abs(d) is how you call abs.
You don't have to import java.lang.Math here, it is done for you.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For static methods you need to include the name of the class.
Also, the <CODE>java.lang</CODE> package is automatically included in every Java program.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!