Results 1 to 7 of 7
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default classes that automatically generate return values

    all,

    I saw someone the other day issue an example of a class with 5 methods in it. one main method and 4 others that issued return values.

    what I saw was that when the class was instanced into a variable and the variable itself was printed out to the debug window, a value was returned. but what I noticed was that it apparently was the value that would result if you called all of the methods in the class in the order in which they were written.

    can someone define this for me? I'm a little confused as to why this would happen. I understand most concepts in Java, but I don't get how instancing a class into a variable and printing it out would automatically trigger the complete execution of the class's methods in sequence.

    Is this just a normal thing with OOP? thanks!

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: classes that automatically generate return values

    Not entirely sure what you mean, do you have a link to the example?

    you mean like when you create a class :

    Java Code:
    public Class example{
    
    private String test;
    
         public Class () {
    
         string x = "Result " + getMehtod1() + " " + getMethod2() + " " +getMethod3();
         System.out.print(x);
    
         }
    }
    when you say like Class c = new Class();

    string x will be printed to the console. obviously if getmethod1,2,3 were written properly.
    Last edited by monkeyjr97; 04-19-2013 at 09:07 PM.

  3. #3
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: classes that automatically generate return values

    Quote Originally Posted by monkeyjr97 View Post
    Not entirely sure what you mean, do you have a link to the example?

    you mean like when you create a class :

    Java Code:
    public Class example{
    
    private String test;
    
         public Class () {
    
         string x = "Result " + getMehtod1() + " " + getMethod2() + " " +getMethod3();
         System.out.print(x);
    
         }
    }
    when you say like Class c = new Class();

    string x will be printed to the console. obviously if getmethod1,2,3 were written properly.
    hi Monkey,

    No I mean something like this:

    Java Code:
    public class number
    {
    
       public static int int1()
       {
          return 1;
       }
    
       public static int int2()
       {
          return 2;
       }
    
       public static void main(String args[])
       {
          number x = new number();
          System.out.println(x);
       }
    
    }
    That code will print out a hex-based memory address. If you call a method in main(), that will alter the printout. But isn't there some rule that all classes have to contain a "toString()" method in them? so for instance, if this was added to that code:

    Java Code:
    public static String toString()
    {
       //some code (not sure what is supposed to be here)
    }
    ...and then add this to the main():

    Java Code:
    x = x.toString();
    doesn't it actually print out a String literal? I'm sorry but I'm missing the connection with all of this. I'm just confused as to why stuff like this is important to OOP. This is just the example I'm using. The semantics are irrelevant, like scope, etc...

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: classes that automatically generate return values

    I am not certain this is what you mean. However, all classes inherit a toString method from Object. Usually it is overridden by the class implementer to return a String of something meaningful. Here is a contrived example:

    Java Code:
    public class SetValues {
       private int a;
       private int b;
       private int c;
    
       public void setA(int v) {
          this.a = v;
       }
    
       public void setB(int v) {
          this.b = v;
       }
    
       public void setC(int v) {
          this.c = v;
       }
    
       public String toString() {
          return "[" + a + "," + b + "," + c + "]";
       }
    
       public static void main(String[] args) {
          SetValues obj = new SetValues();
          obj.setA(5);
          obj.setB(6);
          obj.setC(8);
          System.out.println(obj); // should print [5,6,8]
       }
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: classes that automatically generate return values

    Jim,

    ow would what you wrote be used in a complex program that includes many classes? Can you give an example? Thanks so much. That greatly helps!!

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: classes that automatically generate return values

    Classes can override the toString method to display information for logging and debug purposes. It is also used to print actual values like the wrapper classes for the primitive types.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: classes that automatically generate return values

    A class doesn't generate return values; it is just a collection of variables and/or methods (whether static or not); it's the methods that can possibly return a value after they have been called. The toString() method you're looking at is called in many situations, e.g. when you try to print an instance of a class it is called.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. To pass bean property values to a javascript function and generate dojo datagrid.
    By Chinnu55 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 11-13-2011, 03:06 PM
  2. Replies: 26
    Last Post: 02-25-2010, 11:56 AM
  3. Automatically Generate an XML file
    By svpriyan in forum XML
    Replies: 2
    Last Post: 06-25-2009, 05:46 AM
  4. Replies: 6
    Last Post: 08-06-2008, 09:27 AM
  5. Replies: 2
    Last Post: 07-04-2008, 12:33 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
  •