Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

declaring static methods, enclosing instances

Thread view: 
sovichet.ly@gmail.com - 24 Apr 2007 15:55 GMT
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
}
-----------------------------------------------------------------------------------------------------------------------------------
Lew - 25 Apr 2007 00:18 GMT
> I am working directly out of a beginning programmers book and trying
> to type out this example.  I have two questions.
[quoted text clipped - 9 lines]
> 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
>
[quoted text clipped - 3 lines]
>         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).

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.