Solved

Explain System.out.println

Posted on 2006-11-15
14
1,120 Views
Last Modified: 2010-07-27
Hi Experts,
               I know that Java.lang package is imported by all java programs. In System.out.println, System class extends java.lang and so we are able to use System, without any external imports. But out is an object of PrintWriter class (which is in java.io), please let me know how we are able to use System.out, without an import statement. I mean without importing java.io how are we able to use System.out.
 
              As per my knowledge println is a method of PrintWriter

               Please correct me if i am wrong anywhere in the question.

Thanks in advance

0
Comment
Question by:ramana903
  • 6
  • 4
  • 4
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17945473
You don't need to import PrintWriter - System imports it
0
 

Author Comment

by:ramana903
ID: 17945497
but as per my knowledge only the java.lang package is imported by default, then how is the PrintWriter class imported by default. If the PrintWriter class is imported by default then is there any other class in java.io which is imported by default.

Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17945536
>>then how is the PrintWriter class imported by default.

It isn't. As i just said *System* imports PrintWriter
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:CEHJ
ID: 17945538
... and 'out' is a field of System
0
 
LVL 92

Expert Comment

by:objects
ID: 17945544
0
 
LVL 92

Expert Comment

by:objects
ID: 17945554
>               As per my knowledge println is a method of PrintWriter

It is

> then how is the PrintWriter class imported by default.

It isn't, you only need to import a class if you want to declare a *variable* of that class.
0
 

Author Comment

by:ramana903
ID: 17945602
Thanks for the information, u have mentioned that out is a field, do u mean to say that out is a variable of System?
0
 
LVL 92

Expert Comment

by:objects
ID: 17945606
correct, see the javadoc link I posted above. It shows the details of System class
0
 

Author Comment

by:ramana903
ID: 17945755
ya i have went through the javadoc. So if out is a variable how are we able to call out.println. Means a variable.method...Usually to invoke a method me use object.methodname.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17945756
>>u have mentioned that out is a field, do u mean to say that out is a variable of System?

Yes
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
ID: 17945763
>>Usually to invoke a method me use object.methodname.

'out' *is* an object
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 17945774
> So if out is a variable how are we able to call out.println. Means a variable.method...Usually to invoke a method me use object.methodname.

out is a static variable name

"System.out.println"

System = class name
out = static member variable name
println = method name
0
 

Author Comment

by:ramana903
ID: 17945787
That was great, both of u have helped me to understand it. thanks a lot......:)) I am sharing the points. I had faced this question in an interview, n i had answered it but the interviewer tried to confuse me....anyway next time i will confuse the interviewer....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17945851
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to install java on RHEL image on EC2 4 40
login form jsp example 2 37
jsp insert to database example 2 44
ejb mdb examples 1 7
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question