• Post Reply Bookmark Topic Watch Topic
  • New Topic

System.out.println does not need an import of java.io.*  RSS feed

 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my simple program I can use System.out.println("xxx") without needing to "import java.io.*"

I thought that out.println was part of the java.io package so I don't understand why I don't need to import the package. It would help my understanding of java if someone could explain why this is.

Thanks in advance.
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick reply, using "import java.io.*" saves you from a lot of typing. For example, if you want to use a class from the java.io package, without import statement, you would have to write:-


Coming to your question, "out" is a member of the java.lang.System class and a reference to a java.io.PrintStream object. When you type a statement, such as System.out.println("xxx"); , you are actually accessing the member "out" of the System class, which resides in the java.lang package. java.lang is implicitly imported into every program.
Furthermore, you can use any class from the java.io package, without importing, e.g. java.io.BufferedReader. The import statement simply saves you from typing "java.io.", every-time you access a class from that package.

Hope this answers your question.

Yours,
Abdul Rehman.
[ December 01, 2006: Message edited by: Abdul Rehman ]
 
Tom McCann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for your prompt answer.

Tom
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might have been prompt, but I'm not sure it was 100% correct!

you can use any class from the java.io package, without importing, e.g. java.io.BufferedReader


No you can't. Only java.lang is implicitly imported. You have to import anything in java.io.

Import is needed when you refer explicitly to a particular class, and do not explicitly specify its package when you refer to it. When you refer to a field or method, you do not have to import the class which is the type of the field or the return type of the method.

Finally, a lot of people think importing using wildcard (e.g. import java.io.*) is a bad idea. The reason is that such imports can end up being ambiguous, if more than one imported package has a class of the same name. It is also nice to have an explicit list of dependencies of the class.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Abdul meant was that it is possible to use BufferedReader without importing java.io if one is willing to type the fully qualified classname, i.e. "java.io.BufferedReader", every time.
 
Abdul Rehman
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that was what I meant and I think its pretty clear in my post.

Yours,
Abdul Rehman.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!