The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B(SCJP5) page no:769....... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B(SCJP5) page no:769......." Watch "K&B(SCJP5) page no:769......." New topic

K&B(SCJP5) page no:769.......

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
Source:K&B page no:769

When I am using LINE-2 and commenting the LINE-1,LINE-4
(LINE-3)--MyClass m1=new MyClass();--------------------------it is showing compilation error�
But book says that both import statements will work�

I don�t think it is necessary to set classpath in this case�..

Am I right?

Can anyone explain me?

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Which compilation error is it showing?

xiao fei huang

Joined: Nov 28, 2008
Posts: 7
I tried, just as you said. import in LINE1 works right but LINE2 doesn't. That's strange.

I consider myselft as computer scientist.
Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
it is showing me error as

can not find

MyClass m=new MyClass();
[ November 29, 2008: Message edited by: Ganeshkumar cheekati ]
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

This is the Strange error, I don't know why it happens but if there is only one class in package you cant use Import-on-Demand declaration, ie you cant
use import package.*;

Try to add one more class in your package then use ,

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Originally posted by Ganesh:
public class MyClass { public void hi() { } }

//import; // either import will work //LINE1
import*; //LINE-2
public class Another {
void go() {
MyClass m1 = new MyClass(); // alias name //LINE-3
// m2 = new; // full name //LINE-4

The above code compiles for me if i do the following steps:
1. Compile javac -d .
2. If and are in the same directory then rename to say
3. Compile javac

The problem seems to be the presence of in the current directory from where is being compiled.

As per my understanding the reason is as follows:
When a compiler compiles a class in this case, then it first searches for the referenced class files in the appropriate paths mentioned in the classpath environment variable. (NOTE: My classpath was set to look in current directory first using dot ".")
Since my current directory contained the compiler got confused and thought it was the MyClass.class file and on searching for byte codes it got plain text source code and complained as follows: cannot access MyClass
bad class file: .\
file does not contain class MyClass
Please remove or make sure it appears in the correct subdirectory of the classpath.
MyClass m1 = new MyClass(); // alias name //LINE-3
1 error

So the compiler is not able to resolve the correct class file and got confused.
But if you specify the fully qualified name of the class in the import statement viz. import then the compiler is not confused even if exists in the same directory as

I don't know if it's correct behaviour or a limitation of compiler.

Anybody got a clearer idea please share with us
[ November 29, 2008: Message edited by: Harvinder Thakur ]
I agree. Here's the link:
subject: K&B(SCJP5) page no:769.......
It's not a secret anymore!