Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Static variable problem

843810Jun 13 2005 — edited Jun 13 2005
Hi.. I am new here..

I have written this piece of code to test out sthn.. And errors are generated one saying that non static variable cannot be referenced from a static context and the next says that inner class cannot have static declarations..

Any help would be greatly appreciated.

public class extendsRelationship{

class Square{

protected static int length;

public Square()
{
length=0;
}

public void Square (int l)
{
length=l;
}

public int getLength()
{
return length;
}

public int area()
{
return length*length;
}

public int perimeter()
{
return length*4;
}
}

class Rectangle extends Square
{
private static int breadth;

public Rectangle()
{
length=0;
breadth=0;
}

public void Rectangle(int l, int b)
{
length=l;
breadth=b;
}

public int getBreadth()
{
return breadth;
}

public int area()
{
return length*breadth;
}

public int perimeter()
{
return (length*breadth)*2;
}
}

public static void main (String args[])
{
Square s1= new Square(5);

Rectangle r1=new Rectangle(4,2);

System.out.println("Square side "+s1.getLength()+" has area "+s1.area()+" and perimeter "+s1.perimeter());

System.out.println("Rectangle of length "+r1.getLength()+" and breadth "+r1.getBreadth()+" has area "+r1.area()+" and perimeter "+r1.perimeter());

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 11 2005
Added on Jun 13 2005
1 comment
94 views