Results 1 to 4 of 4
  1. #1
    Jdsfighter is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Set Return Value on User Defined Type

    I'm working on an IRC library, and I have a class named IrcMessage. This class has several functions that are able to parse a string to return a value, however, I'm trying to find a method to return a value when someone just calls the variable without any methods.

    For example:
    Java Code:
    IrcMessage Test = new IrcMessage("PRIVMSG #channel :wooot");
    System.out.println(Test.getKeyword());
    System.out.println(Test.getRecpt());
    System.out.println(Test.getMessage());
    System.out.println(Test);
    The first three work, and return:
    PRIVMSG
    #channel
    wooot

    But I'd like to return: PRIVMSG #channel :wooot if someone calls just Test.

    How can I accomplish this?

  2. #2
    Jdsfighter is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Set Return Value on User Defined Type

    I actually solved my own question, by implementing the method #toString, I was able to make it return properly and automatically.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Set Return Value on User Defined Type

    Well done. Yes, that's how you do it - and thanks for posting your solution (many people don't)

    Just a small point of jargon: you are not so much calling Test as calling System.out.println() with Test. And a less small point of coding style: call the variable test with a small t, reserving capitals for types (like classes). So much more professional! Such things are rather arbitrary - and vary from language to language - but whatever the conventions they enhance readability. (you know a type from a variable without even thinking sbout it) and if it becomes a habit it becomes effortless.

  4. #4
    Jdsfighter is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: Set Return Value on User Defined Type

    Quote Originally Posted by pbrockway2 View Post
    Well done. Yes, that's how you do it - and thanks for posting your solution (many people don't)

    Just a small point of jargon: you are not so much calling Test as calling System.out.println() with Test. And a less small point of coding style: call the variable test with a small t, reserving capitals for types (like classes). So much more professional! Such things are rather arbitrary - and vary from language to language - but whatever the conventions they enhance readability. (you know a type from a variable without even thinking sbout it) and if it becomes a habit it becomes effortless.
    Haha, thanks for the tips, I merely wrote that in a hurry, and wrote it solely on the forum. You'll find, in time, that my conventions are generally much more solid.

Similar Threads

  1. Set of user defined class
    By TelliameD in forum New To Java
    Replies: 4
    Last Post: 07-05-2012, 10:58 AM
  2. Problem with user defined class
    By moosethmucha in forum New To Java
    Replies: 8
    Last Post: 04-17-2012, 06:58 PM
  3. Error: The Type Property Already Defined
    By cest.lavie16 in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 02:32 AM
  4. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 02:02 AM
  5. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 07:13 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
  •