Static variable problem

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());

}

}

[1604 byte] By [ravabolia] at [2007-10-1 16:09:35]
# 1

You cannot have static fields in an inner class.

I suggest you make the classes static.

e.g.

static class Square {

You cannot have a return type for a constructor remove the void before Square(int l);

Then it runs okay on 1.5.0_03

Peter-Lawreya at 2007-7-11 0:15:05 > top of Java-index,Developer Tools,Java Compiler...