ethan codes
ethan codes

Reputation: 87

Java import statements

I was reading about predefined methods and was learning about import statements. I have seen and often times used these to use certain predefined methods, but I've always seen them placed at the very beginning of a program. My question is, can these be placed inside a certain block of code so that it is only seen in that block? I'm not sure that there would ever actually be a reason for this, mostly just curious.

Upvotes: 2

Views: 3397

Answers (4)

Martin Carpella
Martin Carpella

Reputation: 12583

No, you need to define it before of the class/interface, after the package statement.

So an import is always visible to the entire .class-file.

import lets you use members of other packages than your local package, without specifying the full name of a class (e.g. either you need to import java.util.List, or you need to use it's full name everywhere).

There is a tutorial on using package members by Oracle.

The order in a .class-file is defined as:

  1. package specification (optional)
  2. import statements
  3. class / interface / enum definition

Upvotes: 2

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

From Oracle Docs :

Importing a Package Member

To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here's how you would import the Rectangle class from the graphics package created in the previous section.

Upvotes: 0

james_s_tayler
james_s_tayler

Reputation: 1933

My guess is that will give you a compiler error. BUT you can effectively acheive the same thing if you specify the full package name of a class when you instantiate it.

E.g:

public String getString() {
    return new com.package.some.Class("hello world").toString();
}

In this case you don't need to have an 'import' directive at the top of the class because you are telling the compiler inside the method that the class you want is located in the com.package.some package and the class is called Class.

This actually happens in the wild when for example you have to classes in different packages that have the same name. You can only import one of them, the other one you will have to inline the package definition inside the code.

import com.package.some.Class;

public class Yolo {

    private Class classA;
    private com.package.other.Class classB;

    public Yolo(Class classA, com.package.other.Class classB) {
        this.classA = classA;
        this.classB = classB;
    }
}

you can't just import both 'Class' objects and refer to them as Class because the compiler won't know which one. So, this is a valid situation where you will see this kind of thing happen for real.

Upvotes: 0

Nir Levy
Nir Levy

Reputation: 12953

java files contains three parts:

  1. package definition
  2. imports definitions (optional)
  3. the class (or interface/enum) definition.

and it also has to be in this order, you'll get compilation error if it's not in this order

Upvotes: 4

Related Questions