Results 1 to 5 of 5
- 05-14-2011, 11:52 PM #1
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 12
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; } }
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()); } }
- 05-15-2011, 12:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
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.
- 05-15-2011, 12:33 AM #3when an object is passed in like this, do all the class methods run?
Does this just mean we are reassigning different slogans to the same object "obj"
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.
- 05-15-2011, 09:27 AM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 182
- Rep Power
- 12
Sorry it took me so long to reply. Thank you both for those great explanations. It is very clear now. So thank you! Derek
D
- 05-16-2011, 12:27 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 18
Similar Threads
-
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 05:37 AM -
entities are passed by value or passed by reference
By syntrax in forum New To JavaReplies: 1Last Post: 12-17-2009, 08:13 AM -
Can anyone help me to create a INSERT INTO based on a class passed?
By victoreadward in forum New To JavaReplies: 7Last Post: 12-16-2009, 12:51 PM -
println question
By robocop in forum New To JavaReplies: 1Last Post: 03-11-2009, 07:02 AM -
How to use Timer class to schedule a task to execute once 5 seconds have passed
By Java Tip in forum java.utilReplies: 0Last Post: 06-26-2008, 08:32 PM
Bookmarks