I'm new to Java and have been practising programming within the Eclipse IDE. I have the following code:
public static void main(String[] args) {
public double absoluteValue(double n) {
if (n < 0) {
return -n;
} else {
return n;
}
}
System.out.println(absoluteValue(-5));
}
I tried to create a method absoluteValue()
which returns absolute values. However, Eclipse threw a bunch of errors at me.
I managed to get my code to work when I defined absoluteValue()
outside of the main method (had to define it as public static double absoluteValue(double n)
though) and called it within the main method.
But I was just wondering why the above code doesn't work. Can you define methods within the main method (if so, should you)?