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
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
You don't need to import PrintWriter - System imports it
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
Thanks
>>then how is the PrintWriter class imported by default.
It isn't. As i just said *System* imports PrintWriter
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.
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.
ASKER
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
ASKER
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
Yes
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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....
:-)