Static Class Errors

Discussion in 'Java' started by sovichet.ly@gmail.com, Apr 24, 2007.

  1. Guest

    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
    }
     
    , Apr 24, 2007
    #1
    1. Advertising

  2. 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.aspx/java-general/200704/1
     
    Andrew Thompson, Apr 24, 2007
    #2
    1. Advertising

  3. On 2007-04-24, <> 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..
     
    A. Bolmarcich, Apr 24, 2007
    #3
  4. Lew Guest

    <> 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":

    <> 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
     
    Lew, Apr 25, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    960
    Mark Goldin
    Jan 17, 2004
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,407
    Tony Morris
    Oct 19, 2004
  3. E11
    Replies:
    1
    Views:
    4,783
    Thomas Weidenfeller
    Oct 12, 2005
  4. bugbear
    Replies:
    4
    Views:
    439
    bugbear
    Aug 23, 2006
  5. Robin
    Replies:
    0
    Views:
    419
    Robin
    Jun 6, 2007
Loading...

Share This Page