Solved

Explain System.out.println

Posted on 2006-11-15
14
1,126 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

679 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