• Register
1 vote
18 views

Problem:

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

1 Answer

1 vote

Solution:

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)); 
    } 
} 

Thanks.

13 9 6
94,240 points

Related questions

1 vote
1 answer 15 views
15 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 LizzyM 6.1k points
1 vote
1 answer 18 views
18 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 Gavin 15.3k points
0 votes
1 answer 354 views
354 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
1 vote
1 answer 68 views
68 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 Gavin 15.3k points
1 vote
2 answers 43 views
43 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 Gavin 15.3k points