File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B(SCJP5) page no:769......." Watch "K&B(SCJP5) page no:769......." New topic
Author

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?


thanks
Harvinder
xiao fei huang
Greenhorn

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: 2902
    
    1

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:
package com.foo;
public class MyClass { public void hi() { } }


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


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

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

As per my understanding the reason is as follows:
When a compiler compiles a class in this case Another.java, 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 MyClass.java 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:

Another.java:5: cannot access MyClass
bad class file: .\MyClass.java
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 com.foo.MyClass then the compiler is not confused even if MyClass.java exists in the same directory as Another.java

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: http://aspose.com/file-tools
 
subject: K&B(SCJP5) page no:769.......
 
Similar Threads
HashMap
How to make this packages example program work
Unable to set CLASSPATH in Linux-based OS
Clarification on Q6 Self-Test Chapter 3 K&B SCJP5
cannot find symbol error during compilation