Search code examples
javaoutputvoid

How to println value from function?


I would like to println value 5 from void add_up but I cant write name of function: add_up to System out.println(). How to println out of main and get out of void add_up? Thank´s.

import java.io.IOException;

public class function{

    public static void main(String[] args) throws IOException {

            System.out.println(add_up);

    }

    public int add_up(){
    int a = 5;
    return a;
    }
}


Solution

  • In order to call the method you need to add () after its name.

    import java.io.IOException;
    
    public class Function {
    
        public static void main(String[] args) {
          System.out.println(add_up());
        }
    
        public static int add_up(){
          int a = 5;
          return a;
        }
    }
    

    Secondly, you needed to make your method static so the static method main can access it.