declaring static methods, enclosing instances

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

  1. Guest

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

  2. Lew Guest

    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.


    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.

    > -----------------------------------------------------------------------------------------------------------------------------------
    > public class C5E1 {
    > public static void main(String[] args)


    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.

    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3>
    > An inner class is a nested class that is not explicitly or implicitly declared static.


    > Inner classes may not declare static members, unless they are compile-time constant fields (§15.28).


    --
    Lew
    Lew, Apr 25, 2007
    #2
    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. Tor Iver Wilhelmsen
    Replies:
    0
    Views:
    417
    Tor Iver Wilhelmsen
    Jul 18, 2003
  2. Jayaram
    Replies:
    0
    Views:
    434
    Jayaram
    Jul 18, 2003
  3. John Wohlbier
    Replies:
    2
    Views:
    355
    Josiah Carlson
    Feb 22, 2004
  4. bugbear
    Replies:
    4
    Views:
    421
    bugbear
    Aug 23, 2006
  5. Replies:
    8
    Views:
    444
    James Stroud
    Jan 29, 2009
Loading...

Share This Page