Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import mypack.*; (asterisk and without asterisk)

 
Rayudu addagarla
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* class A*/
package pack1;
public class A{
public void call(){
System.out.println("Hello Darling");
}
}
/*******************************************/
/* class B which uses class A by importing pack1*/
import pack1.*;
public class B{
public static void main(String args[ ]){
A obj =new A();
obj.call();
}
}
/***problem *********************/
i am using jdk1.3
"c:\java" current dir.
when compiled at Dos prompt
c:\java> javac -d . A.java --> creates directory pack1-->puts A.class in that dir.
now when compiling B.java
C:\java> javac B.java

C:\Java> javac B.java
B.java:6: cannot resolve symbo
symbol : constructor A ()
location: class A
A obj =new A();
^
B.java:7: cannot resolve symbo
symbol : method call ()
location: class A
obj.call();
^
2 errors
but it works fine and compiles and output IF
i keep
import pack1.A in class B instead of import pack1.*;
i have set the classpath to the current directory
c:\java>set classpath=%classpath%;.;
/* dot indicates current dir */
Can anyone help me the problem the asterisk with and without
 
Mark Ziebell
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry no explanation but an interesting problem.
Perhaps the answer is that it is not good practice to have the A.java in the default package of B when it is not part of the default package.
So the rule is
import pack1.A
does not get confused with A.java in the default package
import pack1.*
recompiles A.java in the default package and puts the class file in the same directory (ie does not use javac -d . when it recompiles A)
Of course using an IDE such as Visual Age for Java prevents you from ever having a java file not in its own package.
Regards
Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic