|
-
December 5th, 2005, 09:55 AM
#1
Printing object variables.
Hi, got a question about printing object variables .
If i create:
main class
*********************
Home first = new Home();
first=23;
System.out.println(home);
*********************'
class Home
{
public double x;
}
Why this prints something like @45de23g and after i add next method in Home class
in prints readable value i.e String value to that unicode.
public String toString()
{
return(x);
}
Can anyone explain why these both cases happen. Even thou i don't call that toString method at all.??
Thanks.
-
December 5th, 2005, 02:14 PM
#2
Re: Printing object variables.
 Originally Posted by claude
Can anyone explain why these both cases happen. Even thou i don't call that toString method at all.??
It's because each and every Java class implicitly extends a class called Object. It's the mother of all classes if you will. The Object class contains a number of public methods of which toString is one so this class will, thanks to inheritance, be available in every class.
Now when the System.out.println finds an object it calls toString of that object to get a String representation of the object which is then printed. If toString is not overridden the toString of Object will be called producing the "strange" string you've noticed. You can read in the documentation what it's supposed to mean. BUT you can also override toString in your own class, like you did, and let toString return whatever you like.
-
December 5th, 2005, 08:50 PM
#3
Re: Printing object variables.
If you override the toString method then it should return a meaningful human readable text description of that particular object. That is the general contract.
What is going on behind the scenary is the concept of virtual methods or virtual functions. When you override the toString method in your class, the other toString method that comes in class Object (which every class inherits) is still there, its just hiding because the OVERRIDDEN toString method is higher in the call chain. There's a bit more to it than just that but it is something you will have to understand sooner or later.
You should investigate the keyword 'super' as well and know how it works.
"The Chicken and Rice MRE is not a personal lubricant."
-
December 5th, 2005, 10:03 PM
#4
Re: Printing object variables.
To print object variables:
There are a few ways to do this, first is if the object variable is declared public, from your main class you could use
Code:
System.out.print(first.x);
however this is usually avoided because classes are usually designed for data protection and most instance variables such as this are declared private.
next, you could use an accessor/mutator method combination.
mutator first: an example would be
Code:
public void setX(double value)
and this would simply set the value of x to the value passed in.
then the accessor method:
Code:
public double getX()
which simply returns the value of x.
and finally in your main class you would call:
Code:
System.out.print(first.getX());
and this would print the returned value of x from the Home class
Also look into constructors as a way to set the values of variables as soon the object is created
Hope this helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|