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.

Static Class Errors

Thread view: 
sovichet.ly@gmail.com - 24 Apr 2007 16:07 GMT
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
}
Andrew Thompson - 24 Apr 2007 17:02 GMT
>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.

Signature

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

A. Bolmarcich - 24 Apr 2007 18:18 GMT
> 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
[quoted text clipped - 5 lines]
> 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..
Lew - 25 Apr 2007 03:53 GMT
sovichet.ly@gmail.com <sovichet.ly@gmail.com> 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
[quoted text clipped - 5 lines]
>> a "no enclosing instance of type C5E1 is accessible.  What am i doing
>> wrong?

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

sovichet.ly@gmail.com <sovichet.ly@gmail.com> wrote:
> 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.

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.