Static variable problem
843810Jun 13 2005 — edited Jun 13 2005Hi.. 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());
}
}