aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes import java.io.* Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "import java.io.*" Watch "import java.io.*" New topic
Author

import java.io.*

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
-------------------------------------
import java.io.*;//line 1
//import java.io.PrintWriter; line 2
//import java.io.OutputStreamWriter; line 3

public class foo {
public static void main(String[] args) throws Exception
{
PrintWriter out = new PrintWriter (new java.io.OutputStreamWriter (System.out), true);
System.out.println("Hello");
}
}
-------------------------------------
The above is foo.java file.
If line 1 is uncommented, it compiles and runs well.
If line 2 is uncommented, it compiles and runs well.
BUT if line 3 is uncommented, it does not compile and the error said symbol could not be resolved...
Can anybody explain this? Thanks a bunch!
Moya
[ August 30, 2002: Message edited by: Moya Green ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
for me it compiled and run ok


SCJP2. Please Indent your code using UBB Code
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Anybody else tried?
I tried on both jdk 1.3 and 1.4. No luck.
Hi Jose, thanks for your prompt reply. Did you copy and paste my code (use
import java.io.OutputStreamWriter; //line 3
in steadof
import java.io.*;//line 1
or
import java.io.PrintWriter; //line 2
)? Sorry to be a pain. Just try to figure out what's wrong with it? Thank you!
Moya
[ August 30, 2002: Message edited by: Moya Green ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I swore it. I did it.
I uncommented the import and all it's right.
Can you write the error message?
Can you check the spelling?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Worked fine for me too. Line 1 being uncommented eliminates the need for line 2 and 3.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Rahul Phadnis
Greenhorn

Joined: Aug 13, 2002
Posts: 7

For me using JDK 1.4, the above code compiles and executes without any modifications.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I retracted my last post after trying it again.
I can confirm that with only line 3 uncommented that I get an unresolved symbol class PrintWriter when using plain vanilla javac 1.4.
Same using JEdit and BlueJ environments.
Using only line 2 the import of java.io.PrintWriter causes the OutputStreamWriter class to be implicitly imported.
Is that what you're at?
-Barry
[ August 31, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I didn't know there was such a thing as "implicit import" in Java. Where can I read more about it?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hey Ron, I didn't mean that as a statement of fact. I guess I should have said "seems as if it had been implicitly imported".
The fact is that just using import java.io.PrintWriter enables a successful compile.
So do you have any idea where the apparent "implicit import (my words)" of OutputStreamWriter comes from?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You don't need to import java.io.OutputStreamWriter at all because you name it explicitly in the one place where you use it.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

Using only line 2 the import of java.io.PrintWriter causes the OutputStreamWriter class to be implicitly imported.

But I can see a java.io before OutputStreamWriter. You don't need to the third import. :roll:
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Yeah well, er, I mean, um, you could be right there... I'll have to investigate that a bit... :roll:
Hey Moya, fine mess you got us into!
I just took out the java.io in the call to the PrintWriter constructor, and guess what? it don't compile anymore.
Well win some, lose a lot...
[ August 31, 2002: Message edited by: Barry Gaunt ]
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Everyone, thank you for your help. I was out of town last weekend and could not join your discussion. I truly appologize for it.
I am sorry I did not say clearly about my question. Barry, you are right! If ONLY line 3 is uncommented, compile errors occurred (like unresolved symbol class PrintWriter...). I don't understand why.
-------------------------------
import java.io.OutputStreamWriter; //line 3
public class Foo {
public static void main(String[] args) throws Exception
{PrintWriter out = new PrintWriter (new java.io.OutputStreamWriter (System.out), true);System.out.println("Hello");
}
}
--------------------------------
Anyway, many thanks again!
Moya
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Because you are not importing the package that PrintWriter is in.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Welcome back, Moya
To summarize, and to put this one to rest.
Notice that OutputStreamWriter is used like this java.io.OutputStreamWriter in the PrintWriter constructor. This means that we do not have to import it. I was blind to that until the other guys practically !!!SHOUTED!!! it at me.
Bearing that in mind, if you uncomment just:
Line 1. then PrintWriter gets imported because you import every class in package java.io by virtue of using the "*".
Line 2. then PrintWriter is imported explicitly.
Line 3. Here we import OutputStreamWriter explicitly, but we do not need to do it. PrintWriter, as Thomas says, has not been imported. Therefore the compiler freaks out.

Cheers,
-Barry
[ September 03, 2002: Message edited by: Barry Gaunt ]
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Barry, Thomas,
I appreciate your help very much. I get it.
-Moya
 
Consider Paul's rocket mass heater.
 
subject: import java.io.*