In Java, this keyword is used to specify that the method has no return value.
The void keyword is used to show that a method will not return a value. // no return type public void setX(int x) { this.x = x; } // returns an int public int getX() { return x; }
A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.
return var_name; e.g int fun() { int x=...; return x; }
It is a syntax error, because a value returning method must return a value, and not writing a return statement with a value is tantamount to returning without a value.
In Java, this keyword is used to specify that the method has no return value.
this is the type of the value that the method returns to its caller
The void keyword is used to show that a method will not return a value. // no return type public void setX(int x) { this.x = x; } // returns an int public int getX() { return x; }
Void is a keyword in Java. It is always used in method declarations. When used in the line that declares a method, it signifies the fact that the method will not return any value. The JVM will not expect the method to return anything. Ex: public static void main(String[] args) {}
A method that return a value should have a return statement. The method signature should indicate the type of return value. While in the case of a method that does not return a value should not have a return statement and in the signature, the return type is void. When using a method that doesn't return a value, a programmer can not get a value from that function, but instead, it can only change variable values and run other methods.
return var_name; e.g int fun() { int x=...; return x; }
It is a syntax error, because a value returning method must return a value, and not writing a return statement with a value is tantamount to returning without a value.
when we declare any function with void,it doesnt return any value
No. void is not a data type. It is mandatory for all java methods to return something and if it is not going to return anything, we have to mark the method with a "void" return type to let the JVM know that it must not expect anything from the method.
Getter method: A getter method have its name start with 'get', and take 0 parameters, and also returns a value. Setter method: A setter method have its name start with "set", and takes 1 parameter. Setters may or may not return a value. Some setters return void, some the value set.
1. A method declared as "int" returns an int value. 2. The main() method in Java is not declared as "int", but as "void", meaning it returns no value.
The final keyword indicates that a variable (identifier) can not change his value. In case the variable refers to a reference variable (an object) the values of variables inside (the object) can change but the reference can be reassigned (to another object).