This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "imports" Watch "imports" New topic
Author

imports

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 90
I have a class
public class foo
{
String name;
public void say();
{
System.out.println("Hi there, "+name);
}
}
and another
import foo;
public class disp
{
public static void main(String arg[])
{
foo f=new foo();
f.name="Ann";
f.say();
}
}
if both these files are in the same directory, then shouldn't these classes work together
I can successfully compile the foo class with the javac by saying javac foo.java
but for some reason i cant compile the other one. So javac disp.java gives me an error saying
disp.java:1:'.' expected
import foo;
1 error ^
whats going on


May the force of the Java be in all of us !!!
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
try taking out the import!!!
it will work if they are in the same directory!
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 90
Okay it does. But even if class foo is in the same directory, the other file class disp still should be told that the class declaration is in another file, shouldnt it? If not, then how is th import statement used to import your own user defined classes?
Any ideas on why I am getting the error listed in the last post?
[ January 14, 2003: Message edited by: Shashank Gokhale ]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Shashank Gokhale:
Okay it does. But even if class foo is in the same directory, the other file class disp still should be told that the class declaration is in another file, shouldnt it? If not, then how is th import statement used to import your own user defined classes?

Your classes can see everything that is in the same package as them. Since you have no "package" statement, you are in the default package, and your classes can see the other classes of yours in the default package.
Remember in Java that a package is basically just a directory, so one way of thinking about it would be that a java class can see the other classes that are in its same directory (but not subdirectories).
Now normally when you develop it's a good idea to place your classes in packages. If you had the line "package mypackage;" at the beginning of both Foo.java and Disp.java, and they were in the directory "mypackage", they would be in the same package and thus able to see each other. Now if for some reason you had Disp.java in package called "myotherpackage", then you would have to place the line...
import myotherpackage.Disp;
...in Foo.java in order for it to be able to find Disp.java. Similarly Disp.java would have to have the line...
import mypackage.Foo;
...in it in order for Disp to use Foo.
Read Understanding Java Packages to help give you a better understanding. Even better, check out Chapter 5 of Bruce Eckel's Thinking in Java which is avilable for free online download.
HTH
[ January 14, 2003: Message edited by: Jason Menard ]
Divya Venkatesh
Greenhorn

Joined: Nov 05, 2002
Posts: 28
There are 2 mistakes
1.Remove the import.The program must work.if it dosent,then your error might be the class path.Set your class path to point to the directory where the files are saved.
2.Remove the ; after public void say()in foo.java.
This is a minor syntactic Error though.

--------------------------------------------------
Have Fun


Divya
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 90
Divya and Jason,
Thanks for your input. It does make sense that you need to use import to import a class that is in a certain package. Can that package be in a different directory?
I have a book here 'Java Black book' by steve holzner that says that to include a class that is a different file, you have to import it into the file that will use this class.
this is what it has:
In printer.java
----------------
public class printer
{
public void print()
{
System.out.prinln("Hello");
}
}
and in app.java
----------------
import printer;
public class app
{
public static void main(String arg[])
{
(new printer()).print();
}
}
Is there any reason why he says that the files will work when they are as shown?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
You do not need to import classes if they are in the same package, even if they are in the same default (unspecified) package. The compiler needs to be able to locate necessary classes through the CLASSPATH setting.
Sun's version 1.4 Java compiler won't allow the import of classes in the default package. I heard a rumor that older versions did allow it, and it may even be true that really old versions required it as you've understood the author to state, but I don't know this to be true. I do know that it isn't necessary (and at least sometimes not allowed) with more recent compilers.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 90
Dirk,
I think youre right in saying that older versions of java did allow the import of classs in the default package.
I understand the concept of imports and packages, I just couldnt figure out why I wasnt able to import a class from the default package.
Thanks
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 90
Divia ,
I typed the ; by mistake in the post when I created it, as in
public void say();
{
It isnt thre in the actual class listing, but actually what the thing was is that 1.4 doesnt allow the import of classs in the default package. So when the book author did this, and I couldnt I was confused as to why I couldnt. It because he was using another java version.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: imports
 
Similar Threads
Bean?
Couple of questions
regarding package concept in java class not found
package concept in java
regarding package concept in java class not found