Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: breaking a method before having a return value

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default breaking a method before having a return value

    Hi again!

    I just can't find a solution for my current problem.
    Is there any solution i can break a running method which is supposed to return an int[] or whatever but !without! any return value.
    I thought that might work with some exception but i didn't find a propper way. To be more specific i want something which tries to find out if a certain field of an object was set and if yes return it and if no returns a message which tells me that the input wasn't made so far.
    something like:

           public int[] returnArray(){
               if(array_was_set==true) return the array;
               else show message that it wasnt set and quit the method without any return value;
           }


    thanks in advance!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,316
    My Mood
    Mellow
    Thanks
    194
    Thanked 671 Times in 658 Posts

    Default Re: breaking a method before having a return value

    You can return null and use that as an indication that it wasn't set.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: breaking a method before having a return value

    Hi.
    When you declare in the method signature that it is returning a data type then it must have a return statement which returns that specific type value. Otherwise you will get compile-time error.
    The only exception when a method can avoid return statement even though it has return type is when there is an infinite loop or an exception is thrown. Otherwise return statement is compulsory.
    Coming to your question, you can easily achieve what you are doing. If you want to terminate at a particular point as per your requirement just say,
    return null;

    It will work for all the data types except for primitive types in which case you need to do type casting to Wrapper class types appropriately.

    Something like this,
        public int[] returnArr()  {
           if(some condition)
             return SomeIntArray;
           else
             return null;
        }
     
        public int returnInt()  {
           if(some condition)
               return 2;
           else
               return (Integer)null;
        }

    Hope that helps.

    Syed.

  4. #4
    Junior Member
    Join Date
    Aug 2013
    Posts
    17
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: breaking a method before having a return value

    I thought there might have been an exception for this but on the other hand i prefer to keep it simple, so that helps

    Thanks both of you!

Similar Threads

  1. return from a method
    By MHS in forum Java Theory & Questions
    Replies: 1
    Last Post: June 15th, 2013, 01:06 PM
  2. Having java return method problem
    By jayleongwk in forum Java Theory & Questions
    Replies: 2
    Last Post: November 4th, 2012, 02:09 AM
  3. [SOLVED] using the return of a method to initialize an ArrayList
    By mia_tech in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 05:05 PM
  4. Why I can't return two variables in one method?
    By netlync in forum Java Theory & Questions
    Replies: 3
    Last Post: January 13th, 2012, 01:39 AM
  5. Method return value(s)
    By mwr76 in forum Loops & Control Statements
    Replies: 3
    Last Post: October 2nd, 2011, 02:38 PM