javaeclipsemathfunc

I'm trying to learn Java, and I was studying the Math library


So, I was trying to run this code: I had to put the private static double() because it didn't run and java kept asking for me to do that

public class Math {
    public static void main(String[] args) {
        
        double z = -9.8;
        double A;

        
        A = Math.abs(z);
        
        System.out.println("Valor absoluto de " + z + " = " + A);
    }

    
    private static double abs(double z) {
        
        double A = abs(z);
        
        return A;
    }
}

and i don't know what it's wrong, help please

please explain why do I have to do the private static and what do I have to write in it


Solution

  • This can be a slightly confusing topic, if you're new to Java.

    First, you'll have to understand the concept of objects in computer science.

    As you may know, coding languages can fall into categories referred to as paradigms.
    Java, along with several others—notably, C++, C#, and Visual Basic—are within, what is termed an object-oriented paradigm.

    In computer science, an object is typically defined as follows.

    "... an object can be a variable, a data structure, a function, or a method."

    As a note, you'll find in most Java literature, an "object" is in reference to a class, and not the variables or methods.
    This is not a standard, it's just a common occurance.

    Your question is regarding why you have to write "private static".

    The static keyword is a context modifier for your method.
    In otherwords, you're negating the need for an instance—the context is static, in terms of its declaration.

    "Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class"

    For example

    Example.methodName();
    

    As, opposed to

    Example example = new Example();
    example.methodName();
    

    Since a class's variables and methods are within the context of a plausible instance, a static modifier will remove it from that context.
    Thus, you cannot access the instance variables and methods, from within a static context.

    Similar to how you can access the Math methods without an instance.

    Consider the following.

    String string = "stack overflow";
    
    static void methodName() {
        /* 'string' will be inaccessible here */
    }
    

    So, in conclusion, you can provide a static modifier to your method, to access it from within the main method, which is static.

    Additionally, you could simply instantiate your enclosing class, and access the non-static method that way.

    public static void main(String[] args) {
        double z = -9.8;
        double A;
        
        Math math = new Math();
        A = math.abs(z);
    
        System.out.println("Valor absoluto de " + z + " = " + A);
    }
    
    private double abs(double z) {
    
        double A = abs(z);
    
        return A;
    }
    

    Furthermore, I believe you mean to have this written.

    private double abs(double z) {
    
        double A = java.lang.Math.abs(z);
    
        return A;
    }