Solved

Explain System.out.println

Posted on 2006-11-15
14
1,093 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 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:
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:

744 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

13 Experts available now in Live!

Get 1:1 Help Now