Static Class Errors

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
}
 
A

Andrew Thompson

I am copying the Rectangle Class right ouf of the book I am studying
from.

What is the name of the book?

I doubt this is how it is in the book. A Rectangle class
is often used to show some basic concepts of OO design,
such as attributes (width/height/color) and methods
(get../set.., findArea). It would complicate the example
to declare Rectangle as an inner class, as shown here.

Most of the problems can be corrected by removing the
'outer' class C5E1 (which is very poorly named).

Note that for those learning Java, a good group is
comp.lang.java.help.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
 
A

A. Bolmarcich

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?
[snip]

What you are doing wrong is that you have Rectangle as an inner class,
but it contains static declarations that an inner class may not have.

Either make Rectangle a top-level class or leave it as a nested class
but declare it as static so it is no longer an inner class.

With the method

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

the keyword "this" is an error because it is not meaningful in a
static method. It would be meaningful in an instance method.

You may want to read about the keyword "this" and nested classes in
book from which you copied the class..
 
L

Lew

A. Bolmarcich said:
What you are doing wrong is that you have Rectangle as an inner class,
but it contains static declarations that an inner class may not have.
You may want to read about the keyword "this" and nested classes in
book from which you copied the class..

You may also want to read the answer I posted the first time you asked this
question, in "declaring static methods, enclosing instances":
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top