Results 1 to 7 of 7
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default Dont understand Return Statement.

    Evening all. I have problem understanding return. I made a simple thing to show what i mean.

    Java Code:
    public class test {
    
    	public test() {
    		init();
    	}
    	
    	public int init() {
    		int i = 10;
    		return i;
    	}
    	
    	public static void main(String[] args) {
    		java.awt.EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				new test();
    			}
    		}
    );
    	}
    
    }
    Can someone be nice and explain what return does. if im not all lost it return the value 10 to init()?

    /ocean

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    If a method returns a value the value is returned to the caller of the method; in your example the caller of the init() method is the constructor of the test class. Try this in your constructor: "System.out.println("init returned: "+init());" and see what happens.

    kind regards,

    Jos

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Aaa, Then i dont need to call the method init(). i can just do this.

    Java Code:
    public class test {
    
    	public test() {
    		System.out.println("init returned: "+init(0));
    	}
    	
    	public int init(int i) {
    	        i = 10;
    		return i;
    	}
    	
    	public static void main(String[] args) {
    		java.awt.EventQueue.invokeLater(new Runnable(){
    			public void run(){
    				new test();
    			}
    		}
    );
    	}
    
    }
    Am i Rigth?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Yes you are right but you are calling the init( ... ) method from your constructor: it is part of the expression that is printed by System.out.println( ... ). If you don't call a method it can't return anything, simple as that ;-)

    kind regards,

    Jos

  5. #5
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Thank you for the well explained lesson. But one more question. Can i only return 1 value? Exampel below dont work.

    Java Code:
    public int init(int i, int a) {
    	    i = 10;
    	    a = 20;
    	   return i;
               return a;
    	 }
    How can that be solved?

    EDIT. But i can return the value of ex. return i+a;
    Last edited by ocean; 10-22-2009 at 12:01 PM.

  6. #6
    literallyjer is offline Member
    Join Date
    Oct 2009
    Posts
    25
    Rep Power
    0

    Default

    You can only return one value. If you want to return more than one, you would have to encapsulate the values into an object or use a collection (like an array or a list.)

    Also, if you don't plan on passing any values into your init() method you can re-write it like this:

    Java Code:
    public int init() {
        int i = 10;
        return i;
    }
    or more precisely:

    Java Code:
    public int init() {
        return 10;
    }

  7. #7
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Now i got it. Thank you. This question has followed me for a long time. :)
    Last edited by ocean; 10-22-2009 at 12:11 PM. Reason: misspell.

Similar Threads

  1. 8 questions I dont understand while studying for SCJP
    By shankhas in forum Java Certification
    Replies: 5
    Last Post: 05-19-2010, 07:53 AM
  2. Help with Recursion and return statement
    By nicolek808 in forum New To Java
    Replies: 3
    Last Post: 09-10-2009, 10:02 AM
  3. Data Files - A problem that I dont understand :D
    By Exhonour in forum New To Java
    Replies: 7
    Last Post: 01-20-2009, 05:13 AM
  4. problem while using return statement
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 06:29 PM
  5. there is no return statement
    By gabriel in forum New To Java
    Replies: 17
    Last Post: 12-03-2008, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •