Results 1 to 7 of 7
- 10-22-2009, 12:19 PM #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.
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, 12:23 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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, 12:30 PM #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.
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, 12:38 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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, 12:52 PM #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.
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 01:01 PM.
- 10-22-2009, 01: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:
Java Code:public int init() { int i = 10; return i; }
Java Code:public int init() { return 10; }
- 10-22-2009, 01: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, 08:53 AM -
Help with Recursion and return statement
By nicolek808 in forum New To JavaReplies: 3Last Post: 09-10-2009, 11:02 AM -
Data Files - A problem that I dont understand :D
By Exhonour in forum New To JavaReplies: 7Last Post: 01-20-2009, 06:13 AM -
problem while using return statement
By shaluchandran in forum New To JavaReplies: 10Last Post: 12-12-2008, 07:29 PM -
there is no return statement
By gabriel in forum New To JavaReplies: 17Last Post: 12-03-2008, 05:55 PM
Bookmarks