'How to call two methods in same class?

How can I call two methods from the same class over one object? I mean I try to write a class and it's methods to run above code:

volume = Calculate.do_calc().get_volume(a);

I am creating Calculate class and two methods of it. do_calc() and get_volume(a). How should I write this class to run that code.



Solution 1:[1]

Unless do_calc() returns the class in where de function get_volume() is located this should never be done.

Solution 2:[2]

Here is a little sample for you.

public class ChainTest {

        public static void main(String[] args) {
                System.out.println(new ChainTest().do_calc().get_volume(1));
        }

        public ChainTest do_calc() {
                // do something;
                return ChainTest.this;
        }

        public int get_volume(int a) {
                return a;
        }
}

Solution 3:[3]

You don't need to write the code in one line. You can call same object methods in different lines.

Calculator calculator = new Calculator();
calculator.do_calc();
calculator.get_volume(a);

In case, if you want static methods

Calculator.do_calc();
Calculator.get_volume(a);

Solution 4:[4]

Case 1 If do_calc is static

public class Calculator {

    public static void main(String[] args) {
        System.out.println(Calculator.do_calc().get_volume(1));
    }

    public static Calculator do_calc() {

        Calculator calculator = new Calculator();
        // do something;

        return calculator;
    }

    public float get_volume(int a) {
        return a;
    }
}

Case 2 : If do_calc is not static

public class Calculator {

    public static void main(String[] args) {
        System.out.println(new Calculator().do_calc().get_volume(1));
    }

    public Calculator  do_calc() {

        Calculator calculator = new Calculator();
        // do something;

        return calculator;
    }

    public float get_volume(int a) {
        return a;
    }
}

Case 3 : If both have return type float as you mentioned in comment

public class Calculator {

    public static void main(String[] args) {

        Calculator calculator = new Calculator();
        calculator.do_calc();
        System.out.println(calculator.get_volume(1));
    }

    public float do_calc() {

        // do something;

        return 1f; // return your result
    }

    public float get_volume(int a) {
        // do something;
        return a;
    }
}

Solution 5:[5]

You must return this; at the end of each method of your class if they are not static. If the methods are static, do it like this:

public class Calculation {
     public static Calculation do_calc () {
           //do your calculation
          return null;
      }

     public static Calculation get_volume(int x) {
           //do your calculation
          return null;
      }
}

Then you can write:

Calculation.do_calc().get_volume(1);

No problem in returning null, as the methods are static and not related to a specific instance of the class. If you don't like it, then return new Calculation();

[Edit]

The first method should return a real object if you need to pass its result to the second method:

public class Calculation {

     int result;

     public static Calculation do_calc () {
           //do your calculation
           Calculation c=new Calculation();
           c.result = theResultOfTheCalculation;
          return c;
      }

     public void get_volume(int x) {
           //do your calculation for example:
           System.out.println(result + x);
      }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ziraak
Solution 2 riversun
Solution 3 sugeesh
Solution 4 Jekin Kalariya
Solution 5