Drew Bartlett
Drew Bartlett

Reputation: 1891

Basic java import question

I need to import "import java.awt.Rectangle. I import it and it tells me the import cannot be resolved. What am I doing wrong?

public class RectangleCalculator {

    import java.awt.Rectangle

Upvotes: 0

Views: 5091

Answers (7)

Jason Rogers
Jason Rogers

Reputation: 19344

I would say put the

import java.awt.Rectangle;

under the package line of the file you need it in

Upvotes: 3

jmq
jmq

Reputation: 10370

Here, read this. It will give you a general overview of packages and imports. It will help you learn the structure of a java source file. Your structure is wrong.

http://www.leepoint.net/notes-java/language/10basics/import.html

EDIT: Try this one too. It is a little more "involved", but has a lot of good details:

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html

Upvotes: 1

Edwin Buck
Edwin Buck

Reputation: 70909

Imports can only be declared in the "Compilation Unit" scope, they cannot be declared in the "Class Scope".

Move that import outside of the class, and everything should work just fine.

Upvotes: 2

mist
mist

Reputation: 1853

write:

import java.awt.Rectanglе;
public class RectangleCalculator {

Upvotes: 3

corsiKa
corsiKa

Reputation: 82559

Turn this

public class RectangleCalculator {

    import java.awt.Rectangle

into this

import java.awt.Rectangle;

public class RectangleCalculator {

Consider what happens when you define an int inside your class:

public class Foo {
    int theFoo;

The compiler says "Oh, I have a new variable, called theFoo, of type int! I had better resolve int!" Well, when you put the import statement inside, it reads it like that and says "Oh, I have a new variable, called java.awt.Rectangle, of type import! I had better resolve import... wait, I can't resolve import :-("

And your compiler gets sad.

Upvotes: 17

Daniel
Daniel

Reputation: 28074

You have to place the import at the top of the file, below the package declaration (which you probably have, no?).

import java.awt.Rectangle;

Don't miss the ; at the end!

Upvotes: 1

Igor
Igor

Reputation: 33973

At the top of your .java file just write

import java.awt.Rectangle

Upvotes: 0

Related Questions