Question: Hi, I have the following error when compiling my java code Shape.java:45: error: non-static variable this cannot be referenced from a static context Circle circle
Hi,
I have the following error when compiling my java code
Shape.java:45: error: non-static variable this cannot be referenced from a static context
Circle circle = new Circle();
^
Shape.java:46: error: non-static variable this cannot be referenced from a static context
Ambiguous ambiguous = new Ambiguous();
^
Shape.java:42: error: Illegal static declaration in inner class JavaShape.IntroductionOverriding
public static void main(String[] args)
^
modifier 'static' is only allowed in constant variable declarations
3 errors
Can you fix my error?
import java.awt.*;
import java.util.*;
class JavaShape
{
public abstract class Shape
{
public void printMe()
{
System.out.println("I'm a shape.");
}
public abstract double computeArea();
}
public class Circle extends Shape
{
private double rad = 5;
public void printMe()
{
System.out.println("I'm a circle.");
}
public double computeArea()
{
return rad * rad * 3.15;
}
}
public class Ambiguous extends Shape{
private double area = 10;
public double computeArea()
{
return area;
}
}
public class IntroductionOverriding
{
public static void main(String[] args)
{
Shape[] shapes = new Shape[2];
Circle circle = new Circle();
Ambiguous ambiguous = new Ambiguous();
shapes[0] = circle;
shapes[1] = ambiguous;
for (Shape s: shapes)
{
s.printMe();
System.out.println(s.computeArea());
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
