2

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.

3
  • 1
    @PritamBanerjee he is refering to Java, your link to Scala. Commented Jan 8, 2016 at 21:08
  • Right , my bad. Copied the wrong one. Commented Jan 8, 2016 at 21:11
  • Others have posted the right answer, but with regards to your statement "I'm not sure that there would ever actually be a reason to do this..." there are in fact good reasons to do these, hence languages like Scala allows it. One example of a reason would be to limit the scope of an import, so that it's only available within a particular block. Commented Jan 8, 2016 at 21:13

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

5 Comments

Worth note that java.lang.* is automatically imported
@cricket_007 true, I changed it to a java.util.List :)
@thatotherguy edited my answer to include the link, thanks
Thanks for the answers! I knew how to use imports in general, I just didn't know if they could only be used this one way or if there was more to them. Thank you again for clarifying!
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.