/******************NEW POST********************************/
Hi guys,
Have you ever thought of:- how the following particular statement gets executed in java, and what's its real meaning:-
public void some_method()
{
....
....
public Object1 obj=new Object1();
System.out.println(obj1);
....
....
....
}
ANSWER:
internally, every object in java extends the "Object" clas, which we can call as the superclass for all the objects in java.
now there is a method "String toString(void)" in "Object" class.
So it gets inherited by all the classes.
the method "toString()" returns the String representation of the object, through which it is called.
When we say:
System.out.println(obj1);
this get internally interpreted as:-
System.out.println( obj1.toString() );
the default implementation of this method in Object returns some cryptic JVM generated code for the object.
Eg:-
for a JFrame object it printed:-
com.fns_issues.gui.Issue_Window[frame0,0,0,400x400,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,5,5,390x390,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16785865,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
com.fns_issues.gui.Issue_Window being the fully qualified class name.
Now we can always override this method in our class of the concerned object.
Simply say:
public String toString()
{
return "hello here i am";
}
and
then:-
System.out.println(obj1);
will print:-
hello here i am.
So, that's all for now. See you guys.
Bye...
/**************************************************************/
No comments:
Post a Comment