In MainActivity class:
Log.i("Game ends! ","" + object1.winner()+ object2.winner());
In Game class:
public void winner() {
if(this.points!=0) {
Log.i("Winner is", this.objectName);
}
}
Error is: '+ operator can no be applied to java.lang.String,void
As per the expertise of the given answers users I have done this.
public String winner() {
if(this.points!=0) {
return this.name;
}
}
error: missing return statement
The
voidkeyword is not an object or a variable, all it does is specify that a method does not return anything. So the following line means that the function will returnvoid(i.e. nothing):Therefore, using the method
Log.i()to print nothing is not possible. This method expects twoStringobjects, which it can print to the logcat. The only reason you can use anintis because of the way Java handlesStringconcatenation; it automatically converts any primitive type (boolean,int,float, etc.) to aStringwhen using the+operator.The reason the
Logmethods expects twoStringobjects is simply to maintain legibility in logcat. The first parameter is meant to be aTAGvariable, which is usually the name of theClassthat is calling the method. This is usually done by creating a static value in each class:In this way, you can use the
Logmethods as follows:Which will result in a more uniform message written to logcat.
So, with this information, the question remains; what are you trying to output to logcat?
If you just want to output that the method was called, than simply call the
Logstatement before (or after) the method call:Otherwise, if you actually expect your method to
returna printable result, then you must return something other thanvoid. Either aString, a primitive type, or anObjectthat can be printed using thetoString()method.