Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Query:function of method toString()?

Thread view: 
Jack Dowson - 10 May 2007 10:08 GMT
Hello Everyone:
I came across a demo yesterday,which use System.out.println(cla) to
print a sentence on screen.But "cla" here is a class name.There is only
one method in this class definede as:public String toString().
Here is my question:If a mehod like "public String to String()" defined
in a class,then the class name will represent a string in a String
context,right?

Any help will be greatly appreciated!
Dowson.
Patricia Shanahan - 10 May 2007 10:58 GMT
> Hello Everyone:
> I came across a demo yesterday,which use System.out.println(cla) to
[quoted text clipped - 6 lines]
> Any help will be greatly appreciated!
> Dowson.

Java does not have a concept of "X context".

PrintStream has several println methods, including one that takes an
Object argument. If cla is a reference to some object,
System.out.println(cla) prints the result of that object's toString
method. That happens regardless of what other methods the class has.

EVERY object has a public String toString() method, either declared or
inherited, because Object declares public String toString().

However, I am not sure what you mean by '"cla" is a class name. The
following program, with a class name as println argument, does not
compile because of a syntax error:

public class MyClass {
  public static void main(String[] args){
    System.out.println(MyClass);
  }
}

You can call println for the Class object associated with a class:

System.out.println(MyClass.class)

That prints the result of the toString method for the Class object for
MyClass, the word "class" followed by a space and the name of the class.

Note: Watch the capitalization. "class" means the general concept of a
class. "Class" means the class of the object the

Patricia
Jack Dowson - 10 May 2007 12:22 GMT
Thank you so much,Patricia Shanahan!!!

Dowson.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.