Results 1 to 6 of 6

Thread: In multiple returns which one gets the final return value?

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    In multiple returns which one gets the final return value?

    Hello to all,
    I recently started learning java language. I have one problem in following code.
    Code:
    String test() {
        try {
            return "one";
        } finally {
            return "two";
        }
    }
    I want to asked whether above code define the return value of a call to test()? It means in multiple returns which one gets the final return value? Please help me.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: In multiple returns which one gets the final return value?

    As per my information from above code in multiple returns two is the return value. It means language specification defines that "2" is the result. It is does not depend on Virtual machine, because it is not spec-compliant. If you are tried to execute above code then return block will never be executed. In this case try to write simple code to avoid such situation.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: In multiple returns which one gets the final return value?

    Hey remember that the finally block will always be executed last, but not in the program like following
    Code:
    String testEg() {
        try {
            Systems.exit(0);
        } finally {
            return "two";
        }
    }
    In above code the java virtual machine will stop without executing the finally block. In your above code the return value will be 2.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: In multiple returns which one gets the final return value?

    When you use finally block in your code along with return function, the value return by finally block replace whatever you might have returned from the try or catch block. The same type of concept is apply for exceptions. Whenever you throw value in the finally block, that particular exception will replace all values was thrown in the try or catch block. So be careful while passing any value to finally block.

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: In multiple returns which one gets the final return value?

    From you information your code returns value "2". I think finally block will never execute. If you are tried to run this code in Visual Studio, then it will show an warning telling it as unreachable code. If you execute this code normally then it will not give any error message. If you tried to run code in Netbeans, the you will get warning: "Finally clause cannot complete normally".

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: In multiple returns which one gets the final return value?

    You have written wrong code and that's why you are getting such type of problem. In this case you have to write following code to get rid out of this problem. In the following code I have use StudentEg class to include all methods.
    Code:
    package onlineResult;
    
    public class StudentEg
    {
       String names;
       String grades;
       String courses;
    
       public StudentEg()
       {}
    
       public Student(String names,String grades,String courses)
       {
          this.names = names;
          this.grades = grade;
          this.courses = course;
       }
    
       public String getName()
       {
          return names;
       }
    
       public void setName(String names)
       {
          this.names = names;
       }
       public String getGrade()
       {
          return grades;
       }
    
       public void setGrade(String grades)
       {
          this.grade = grades;
       }
       public String getCourse()
       {
          return courses;
       }
    
       public void setCourse(String courses)
       {
          this.courses = courses;
       }
    
    }

Similar Threads

  1. Replies: 3
    Last Post: 21-01-2012, 01:43 AM
  2. Multiple Matches Return To VLOOKUP
    By Brunon in forum Windows Software
    Replies: 5
    Last Post: 26-11-2010, 11:32 PM
  3. Method with multiple return types
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 05:10 AM
  4. Replies: 5
    Last Post: 27-10-2009, 12:40 AM
  5. php return multiple values
    By Aston5 in forum Software Development
    Replies: 2
    Last Post: 29-06-2009, 10:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,753,713.26280 seconds with 17 queries