Results 1 to 7 of 7
- 10-22-2009, 11:19 AM #1
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Dont understand Return Statement.
Evening all. I have problem understanding return. I made a simple thing to show what i mean.
Can someone be nice and explain what return does. if im not all lost it return the value 10 to init()?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(); } } ); } }
/ocean
- 10-22-2009, 11:23 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-22-2009, 11:30 AM #3
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Aaa, Then i dont need to call the method init(). i can just do this.
Am i Rigth?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(); } } ); } }
- 10-22-2009, 11:38 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-22-2009, 11:52 AM #5
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Thank you for the well explained lesson. But one more question. Can i only return 1 value? Exampel below dont work.
How can that be solved?Java Code:public int init(int i, int a) { i = 10; a = 20; return i; return a; }
EDIT. But i can return the value of ex. return i+a;Last edited by ocean; 10-22-2009 at 12:01 PM.
- 10-22-2009, 12:00 PM #6
Member
- Join Date
- Oct 2009
- Posts
- 25
- Rep Power
- 0
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:
or more precisely:Java Code:public int init() { int i = 10; return i; }
Java Code:public int init() { return 10; }
- 10-22-2009, 12:06 PM #7
Senior Member
- Join Date
- Sep 2009
- Location
- Sweden/Borås
- Posts
- 107
- Rep Power
- 0
Similar Threads
-
8 questions I dont understand while studying for SCJP
By shankhas in forum Java CertificationReplies: 5Last Post: 05-19-2010, 07:53 AM -
Help with Recursion and return statement
By nicolek808 in forum New To JavaReplies: 3Last Post: 09-10-2009, 10:02 AM -
Data Files - A problem that I dont understand :D
By Exhonour in forum New To JavaReplies: 7Last Post: 01-20-2009, 05:13 AM -
problem while using return statement
By shaluchandran in forum New To JavaReplies: 10Last Post: 12-12-2008, 06:29 PM -
there is no return statement
By gabriel in forum New To JavaReplies: 17Last Post: 12-03-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks