Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Static Class Errors

Reply
Thread Tools

Static Class Errors

 
 
sovichet.ly@gmail.com
Guest
Posts: n/a
 
      04-24-2007
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
}

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      04-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>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.asp...neral/200704/1

 
Reply With Quote
 
 
 
 
A. Bolmarcich
Guest
Posts: n/a
 
      04-24-2007
On 2007-04-24, (E-Mail Removed) <(E-Mail Removed)> wrote:
> 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..
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-25-2007
(E-Mail Removed) <(E-Mail Removed)> wrote:
>> 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?


A. Bolmarcich wrote:
> 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":

(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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.


--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
referring to static fields of enclosing class from static inner class? bugbear Java 4 08-23-2006 08:26 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments