Sign in to follow this  

[java] Importing to several files

This topic is 4586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know I should know this, but it was never mentioned in my classes. What happens if I do this import java.foo.bar; in several different files? Does the Java compiler have inbuilt guards against including the same code in two different places; or perhaps an import in Java isn't really equivalent to an #include in C++? In fact, just what does an import statement do apart from making the compiler aware of where to find stuff?

Share this post


Link to post
Share on other sites
You can use the same import in as many files as you'd like. Think of it as being local to the particular source module:


// MyClass.java
package mypackage;

class MyClass {
public MyClass() {
}
}

// AnotherClass.java
import mypackage.MyClass;
class AnotherClass {
MyClass mc;
}

// YetAnotherClass.java
import mypackage.MyClass;
class YetAnotherClass {
MyClass mc;
}

// Foo.java
class Foo {
MyClass mc;
}




Even if all of the above files are part of the same project, AnotherClass and YetAnotherClass will compile just fine. Foo.java will not compile though, because MyClass is not 'visible' to it. Add an import statement and it will compile fine.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by King of Men
I know I should know this, but it was never mentioned in my classes. What happens if I do this

import java.foo.bar;

in several different files? Does the Java compiler have inbuilt guards against including the same code in two


Import does not include any code at all.

You are getting confused with a completely unrelated function usually found in scripting languages or in C preprocessors that copies the contents of file X and places them here inline.

Import is purely to do with namespacing, and enables you to explicitly say "I'm using the following library, so wherever I use a class name you don't recognize, go look in that library for it, so that I don't have to bother typing out the full name of the class. Oh, and by the way, if you ever aren't sure which class I meant (because I have two libraries with the same named class in them), then give me a compiler error and tell me which ones they were so I can go back and tell you exactly which one I meant".

redmilamber

Share this post


Link to post
Share on other sites

This topic is 4586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this