Results 1 to 6 of 6
  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Question Trying to receive a return

    Hello all! :D

    I have been learning about return statements but I can't remember how to receive the return after the variable has been returned. example:

    Java Code:
    public static String name(){
    return ("hello");
    
    }
    I think the constructor has something to do with it and it would be something like name(string h); in order to use the returned string? correct if i am wrong.

    Anyways thanks :D
    Last edited by rizowski; 01-21-2011 at 01:31 AM. Reason: added detail

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your question is a bit unclear, a method can return a value and you can use that return for multiple things, such as storing it in some variable or printing it.
    Java Code:
    public static String name(String str){
      return str;
    }
    with this you can call the method with a string as the argument and it will return that argument
    Java Code:
    System.out.println(name("Hello"));//prints the string argument
    String s = name("hello");//sets s to hello

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Your first set of code is what I am talking about. But how would i make another method be able to use the return of String str?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    it depends what you want the other code to do, if you want it to print some value you would use:
    Java Code:
    System.out.println(name("hello"));
    if instead you want to set a string with the return value:
    Java Code:
    String s = name("hello");
    if something returns a string you can think of using the method as some string type.

  5. #5
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Oh cool alright i understand now! Thanks :D

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im glad, don't be afraid to come back here and ask more questions if you are stuck on something.

Similar Threads

  1. not able to receive packets?? pls help.
    By abdullahansari in forum New To Java
    Replies: 1
    Last Post: 06-24-2010, 02:19 AM
  2. UDP Receive Problem
    By Debbie in forum Networking
    Replies: 0
    Last Post: 04-27-2010, 10:56 AM
  3. How to receive mail using JMS
    By ashoksaravanan in forum Web Frameworks
    Replies: 1
    Last Post: 02-22-2010, 10:11 AM
  4. How to send and receive SMS to and from JSP
    By ashrafraheem in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:16 AM
  5. Replies: 0
    Last Post: 12-02-2008, 10:55 AM

Posting Permissions

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