Reputation: 1891
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
Reputation: 19344
I would say put the
import java.awt.Rectangle;
under the package line of the file you need it in
Upvotes: 3
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
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
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
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