S
sovichet.ly
I am copying the Rectangle Class right ouf of the book I am studying
from. Why do I get errors declaring color to be static. I get " The
field color cannot be declared static, fields can only be declared in
static or top level types". Same for the two methods that use static
color.
In addition, when I try to create an instance to test Rectangle. I get
a "no enclosing instance of type C5E1 is accessible. What am i doing
wrong?
--------------------------------------------------------
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
}
from. Why do I get errors declaring color to be static. I get " The
field color cannot be declared static, fields can only be declared in
static or top level types". Same for the two methods that use static
color.
In addition, when I try to create an instance to test Rectangle. I get
a "no enclosing instance of type C5E1 is accessible. What am i doing
wrong?
--------------------------------------------------------
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
}