Link to home
Start Free TrialLog in
Avatar of ramana903
ramana903

asked on

Explain System.out.println

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You don't need to import PrintWriter - System imports it
Avatar of ramana903
ramana903

ASKER

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
>>then how is the PrintWriter class imported by default.

It isn't. As i just said *System* imports PrintWriter
... and 'out' is a field of System
>               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.
Thanks for the information, u have mentioned that out is a field, do u mean to say that out is a variable of System?
correct, see the javadoc link I posted above. It shows the details of System class
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.
>>u have mentioned that out is a field, do u mean to say that out is a variable of System?

Yes
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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....
:-)