Solved

Explain System.out.println

Posted on 2006-11-15
14
1,139 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

627 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