问题
I use dr java for linux to run my code. I've been running into a bunch of errors recently while running this simple program. The code compiles fine but whenever I run the java file, I get the error; Static Error: Undefined name here's my code; (I've got two files, Square.java and SquareD.java) Here's Square.java
public class Square{
private String name;
private int y;
private int x;
public Square(String st,int x2,int y2){
name=st;
x=x2;
y=y2;
}
public int square(){
return x*x+y*y;
}
double a1=(double) x;
double a2=(double) y;
public double hypotenuse(double a1,double a2){
double sum = a1*a1+a2*a2;
return Math.sqrt(sum);
}
public int area(){
return x*y;
}
public String getName(){
return name;
}
}
Here's SquareD.java;
public class Square{
private String name;
private int y;
private int x;
public Square(String st,int x2,int y2){
name=st;
x=x2;
y=y2;
}
public int square(){
return x*x+y*y;
}
double a1=(double) x;
double a2=(double) y;
public double hypotenuse(double a1,double a2){
double sum = a1*a1+a2*a2;
return Math.sqrt(sum);
}
public int area(){
return x*y;
}
public String getName(){
return name;
}
}
Whever I type java Square.java
or java SquareD.java
in the interactions window, I get,
Static Error: Undefined name
I'm running Ubuntu 11.10
回答1:
You don't run a class by typing java Square.java
, but java Square
.
(Or run Square
, the equivalent of hitting the Run button.)
But you'll still need a main
method with the following signature:
public static void main(String[] args) {
// etc.
}
Then when you type java Square
(assuming you've compiled it) it will run whatever is in main
.
来源:https://stackoverflow.com/questions/12546071/why-does-drjava-return-a-static-error