E
E
ehordyenko2021-10-06 13:11:31
Java
ehordyenko, 2021-10-06 13:11:31

How to correctly call the void method in main?

this is how i try to call :
System.out.println(real.increment(imaginary));

here is the error :

error: Class names, 'javac', are only accepted if annotation processing is explicitly requested
ComplexMain.java:14: error: incompatible types: void cannot be converted to Complex
                Complex res = real.increment(imaginary);


here is the method:
public void increment(Complex other) {
this.real += other.real;
this.imaginary += other.imaginary;
}


I tried like this:
System.out.println(increment(imaginary));

but alas, it doesn't work((

what's the right way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-10-06
@ehordyenko

Error in the line
Complex res = real.increment(imaginary);
and in
System.out.println(real.increment(imaginary));
the same error. The increment method returns void, and changes the internal value. void cannot be assigned to anything, and cannot be passed anywhere. You can consider it "emptiness", "nothing". The void method does not return anything, so its return value cannot be assigned anywhere.
You need to split this into different lines:

real.increment(imaginary);
System.out.println(real);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question