Results 1 to 5 of 5
  1. #1
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default Class object passed into println(obj) question

    Hi, I have the following code


    Slogan obj;

    obj = new Slogan ("Remember the Alamo.";
    System.out.println (obj);

    and the Slogan class has a few public methods and a constructor, i was wondering when an object is passed in like this, do all the class methods run? It looks like they do. After the constructor is called, there is a method that returns "Remember the Alamo".

    Also, consider this please:

    obj = new Slogan ("Don't Worry. Be Happy.");
    System.out.println (obj);

    Does this just mean we are reassigning different slogans to the same object "obj". Any help Greatly appreciated. Thanks again. derek:D



    Here is the full code:

    Java Code:
    //********************************************************************
    //  Slogan.java       Author: Lewis/Loftus
    //
    //  Represents a single slogan string.
    //********************************************************************
    
    public class Slogan
    {
       private String phrase;
       private static int count = 0;
    
       //-----------------------------------------------------------------
       //  Constructor: Sets up the slogan and counts the number of
       //  instances created.
       //-----------------------------------------------------------------
       public Slogan (String str)
       {
          phrase = str;
          count++;
    	  
       }
    
       //-----------------------------------------------------------------
       //  Returns this slogan as a string.
       //-----------------------------------------------------------------
     public String toString()
       {
          return phrase;
       }
    
       //-----------------------------------------------------------------
       //  Returns the number of instances of this class that have been
       //  created.
       //-----------------------------------------------------------------
       public static int getCount ()
       {
          return count;
       }
    }
    and here


    Java Code:
    //********************************************************************
    //  SloganCounter.java       Author: Lewis/Loftus
    //
    //  Demonstrates the use of the static modifier.
    //********************************************************************
    
    public class SloganCounter
    {
       //-----------------------------------------------------------------
       //  Creates several Slogan objects and prints the number of
       //  objects that were created.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          Slogan obj;
    
          obj = new Slogan ("Remember the Alamo.");
          System.out.println (obj);
    
          obj = new Slogan ("Don't Worry. Be Happy.");
          System.out.println (obj);
    
          obj = new Slogan ("Live Free or Die.");
          System.out.println (obj);
    
          obj = new Slogan ("Talk is Cheap.");
          System.out.println (obj);
    
          obj = new Slogan ("Write Once, Run Anywhere.");
          System.out.println (obj);
    
          System.out.println();
          System.out.println ("Slogans created: " + Slogan.getCount());
       }
    }

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

    Default

    Slogan obj;

    obj = new Slogan ("Remember the Alamo.";
    System.out.println (obj);

    and the Slogan class has a few public methods and a constructor, i was wondering when an object is passed in like this, do all the class methods run?

    No - println() will just invoke the toString() method of Slogan to determine what to print for that partticular object.

    It looks like this:

    Java Code:
    Slogan obj;
    
        // calls the constructor
        // as a result the phrase is set and the count is updated
    obj = new Slogan ("Remember the Alamo.";
        // println() calls the toString()
        // which returns the phrase set earlier
    System.out.println (obj);

    (It's not terribly important at this time, but the behaviour of println() is documented in the PrintStream and String API docs. A nice point is that things are handled well when the value of a Slogan variable is null.)

    -----------------------

    Also, consider this please:

    obj = new Slogan ("Don't Worry. Be Happy.");
    System.out.println (obj);

    Does this just mean we are reassigning different slogans to the same object "obj".

    Again, no. The "new" keyword was chosen deliberately: it creates a new object. So the constructor assigns a value to phrase, but this phrase "belongs to" a wholly new, different and distinct object.

    The fact that you use the same variable (obj) to refer first to one object and then to another new one does not alter the fact that there are two objects.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    when an object is passed in like this, do all the class methods run?
    No. The default method called in the println() when you don't specify a method is the toString() method.

    Does this just mean we are reassigning different slogans to the same object "obj"
    Yes and No.
    obj is a reference to an object. Its value changes with each assignment statement.
    obj is the same variable but the object it points to is changed by the assignment statement.

    Same as:
    int x = 4; // set value of x
    x = 5; // change value of x

    One variable: x, has different values after each assignment statement.

  4. #4
    silverglade is offline Senior Member
    Join Date
    Feb 2009
    Posts
    182
    Rep Power
    6

    Default

    Sorry it took me so long to reply. Thank you both for those great explanations. It is very clear now. So thank you! DerekD

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

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. entities are passed by value or passed by reference
    By syntrax in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 07:13 AM
  3. Replies: 7
    Last Post: 12-16-2009, 11:51 AM
  4. println question
    By robocop in forum New To Java
    Replies: 1
    Last Post: 03-11-2009, 06:02 AM
  5. Replies: 0
    Last Post: 06-26-2008, 07:32 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
  •