Solved

Explain System.out.println

Posted on 2006-11-15
14
1,105 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running Jira on Raspberry PI 2? 3 267
Python Assistance 7 73
xampp tool 12 28
MySqlDump not dumping triggers 1 19
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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now