declaring static methods, enclosing instances

S

sovichet.ly

I am working directly out of a beginning programmers book and trying
to type out this example. I have two questions.

1) For the Rectangle class eclipse gives me the error of "The field
color cannot be declared static; static fields can only be declared in
static or top level types" It gives me the same errors for the
methods getColor and setColor. I thought that I declared this in the
top level already for the Rectangle class. Once again, this is
directly out of the book(no errata, so I'm confident it's me).

2) I am attempting to create an instance of Rectangle. It gives me
the error that no enclosing instance type C5E1 is accessable. Must
qualify the allocation with an enclosing instance of type C5E1.

Any help would be appreciated.

-----------------------------------------------------------------------------------------------------------------------------------
public class C5E1 {
public static void main(String[] args)
{
//create rectangle
Rectangle r1 = new Rectangle();




}//end main



public class Rectangle
{
private double width = 1;
private double height = 1;
private static String color = "white";

public Rectangle()
{// default constructor
}//end Rectangle

public Rectangle(double width, double height, String color)
{
}//end Rectangle

public double getWidth()
{
return width;
}//end getWidth

public void setWidth(double width)
{
this.width = width;
}//end setWidth

public double getHeight()
{
return height;
}//end getHeight

public void setHeight(double height)
{
this.height = height;
}//end setHeight

public static String getColor()
{
return color;
}//end getColor

public static void setColor(String color)
{
this.color = color;
}//end setColor

public double findArea()
{
double area = this.height * this.width;
return area;
}//end findArea
}//end class rectangle
}
-----------------------------------------------------------------------------------------------------------------------------------
 
L

Lew

I am working directly out of a beginning programmers book and trying
to type out this example. I have two questions.

1) For the Rectangle class eclipse gives me the error of "The field
color cannot be declared static; static fields can only be declared in
static or top level types" It gives me the same errors for the
methods getColor and setColor. I thought that I declared this in the
top level already for the Rectangle class. Once again, this is
directly out of the book(no errata, so I'm confident it's me).

2) I am attempting to create an instance of Rectangle. It gives me
the error that no enclosing instance type C5E1 is accessable. Must
qualify the allocation with an enclosing instance of type C5E1.

You declared Rectangle as an inner class. That means it requires an instance
of the enclosing class to exist. You did not create such an instance.

It is not wise to embed TAB characters in Usenet posts.
{
//create rectangle
Rectangle r1 = new Rectangle();

What you need is
Rectangle r1 = new C5E1().new Rectangle();
}//end main

public class Rectangle
{
private double width = 1;
private double height = 1;
private static String color = "white";

You cannot declare non-final, non-compile-time-constant static fields in inner
classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top