• Register
1 vote


I admit it that Java programming is fun but it seems confusing sometimes. I am trying to adopt somethings about method and object in java. I know how to create a method and calling it inside the main method. I have a very simple question, can a void method have a return statement? What happens if I try to return values from a void method?
A well-explanation is expected. Thanks again, guys.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote


In Java, the keyword return used as a special and reserved keyword, not as a modifier. It is used at the end of any function with or without a value. Basically, it helps to exit a method.

As per your question, Yes, a void method can have a return statement as well as it can return a value. Let see:

public class MethodTest { 
    void myMethod(int x, int y){ 
        int sum = 0; 
        sum = (x + y); 
        return sum;
    public static void main(String[] args{ 
        System.out.println(new MethodTest().myMethod(5,6)); 


13 9 6
94,240 points

Related questions

1 vote
1 answer 19 views
Problem: I am a beginner in programming language and I hvae been doing some coding practice and learning some programing logics .So, can anyone explain me the return statement in a void method of python?
asked Apr 8, 2020 LizzyM 6.1k points
1 vote
1 answer 40 views
Problem: I am exploring the c++ programming language nowadays. It’s really interesting to learn new things every day. I was trying to adopt the methods in c++ but struggling to return values from a void method. I actually want to know that if it is ... a return statement in a void method. Thanks for your co-operation in this learning process. I really appreciate your effort. Thanks again.
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 656 views
Problem : I am trying to learn Java, I am very newbie however I wanted to know what I do to get the "run" it. I am facing below error: Static Error: This class does not have a static void main method accepting String[]. Below is my code: public class MyCaneirinho { public static void mycontar ... { System.out.println(j + x + y); } else { System.out.println(j + x + y + z); } } } } // MyCarneirinho
asked Dec 10, 2019 alecxe 7.5k points
0 votes
1 answer 12 views
Problem: Hello there! I am working with C# in Visual Studio and I am learning user-defined functions in C#. When I try to execute my program, it throws an error that says method must have a return type. I have checked my function and I find nothing wrong with it. ... ;t know what’s the problem and what should I do. If someone could tell me, that would be a big favor. Thanks in advance
asked Oct 16, 2020 Code Learner 6k points
1 vote
1 answer 36 views
Problem: Let me put it as sim as I can. I have an if statement in my code and it returns a value. Since IF is a boolean statement it should return value whether it’s true or false. So, in my program when a method tests an argument and returns a true or ... as a parameter in a different method inside an IF statement. How should I do that? Any kind of help is appreciated by my side. Thanks.
asked Mar 17, 2020 Gavin 15.3k points
0 votes
1 answer 7 views
What am I doing wrong?
asked Jan 24 TeamScript 13.5k points
0 votes
1 answer 31 views
Why is this so?
asked Jan 1 TeamScript 13.5k points
1 vote
1 answer 202 views
Problem: “This method must return a result of type boolean” What does it mean? I have a sample java program but I can’t run my program due to the above error. Once I try to run the program it gives back the error. Please find my code snippet below to understand my problem: ... && board[n] == board[n+1] && board[n] == board[n+2]) { return true;} } } Thanks for your valuable time
asked Mar 28, 2020 Gavin 15.3k points
1 vote
2 answers 69 views
Problem: Hello Kodlogs, I have a simple java code that I wrote a few minutes back I was trying to compare numbers using methods. The program throws the error again and again: this method must return a result of type int. I couldn’t figure out the problem. Could you please shed some light on this ... []) { int x = 8; int y = 9; int z = 20; System.out.println(largest(x, y, z)); } } Many thanks
asked Mar 27, 2020 Gavin 15.3k points