简体   繁体   中英

Java: non-static variable cannot be referenced from a static context

  package lab10;

  import java.util.*;
  public class Lab10 {

  class QuadraticEquation{

   double a,b,c;

   QuadraticEquation(){
     Random number1 = new Random();
     a = (int) (number1.nextDouble() * 8 + 1.0);

     Random number2 = new Random();
     b = (int) (number2.nextDouble() * 8 + 1.0);

     Random number3 = new Random();
     c = (int) (number3.nextDouble() * 8 + 1.0);


   }
   double getA(){

       return a;
   }
   double getB(){

       return b;
   }
   double getC(){      

       return c;
   }
   double getDiscriminant(){
       double discriminant;

       discriminant = (Math.pow(b, 2)- 4 *(a *c));
       return discriminant;
     }
   }

public static void main(String[] args) {

    QuadraticEquation equation = new QuadraticEquation(); //<---- error

    System.out.println("Three randomized coefficients are: ");
    System.out.println("a = " + equation.getA());
    System.out.println("b = " + equation.getB());
    System.out.println("c = " + equation.getC());

}
  }

I've read a couple of posts here on stackoverflow about this error but even after reading them I don't seem to really understand why I'm getting it. I'm referencing a previous project and I don't understand why I cant declare QuadraticEquation equation = new QuadraticEquation()

Here is the project I'm referencing:

   package InClass05;

   public class TestSimpleSquare {

   public static void main(String[] args) {

    SimpleSquare square1 = new SimpleSquare();
    System.out.println("The area of the square of side lengths " + square1.length + " is " + square1.getArea());
    System.out.println("The perimeter of the square of side lengths " + square1.length + " is " + square1.getPerimeter());
    System.out.println("The diagonal of the square of side lengths " + square1.length + " is " + square1.getDiagonal());

    System.out.println("");
    SimpleSquare square2 = new SimpleSquare(25);
    System.out.println("The area of the square of side lengths " + square2.length + " is " + square2.getArea());
    System.out.println("The perimeter of the square of side lengths " + square2.length + " is " + square2.getPerimeter());
    System.out.println("The diagonal of the square of side lengths " + square2.length + " is " + square2.getDiagonal());

    System.out.println("");
    square2.length = 500;
    System.out.println("The area of the square of side lengths " + square2.length + " is " + square2.getArea());
    System.out.println("The perimeter of the square of side lengths " + square2.length + " is " + square2.getPerimeter());
    System.out.println("The diagonal of the square of side lengths " + square2.length + " is " + square2.getDiagonal());


}
  }
  class SimpleSquare{
  double length;

   SimpleSquare(){
    length = 10;
}

SimpleSquare(double newlength){
    length = newlength;
}

double getArea(){
    return length * length;
}

double getPerimeter(){
    return length + length + length + length;
}

double getDiagonal(){
    return Math.sqrt(Math.pow(length, 2) + Math.pow(length, 2));
}
void setlength(double newlength){
    length = newlength;
}
 }

In this one I'm able to declare SimpleSquare square1 = new SimpleSquare(); I don't get why I can't do that in the project I'm working on now. Any help would be appreciated. Thank You.

It should be:

QuadraticEquation equation = new Lab10().new QuadraticEquation();

because QuadraticEquation is inner class

You could add the word static to make your class static:

static class QuadraticEquation

Without this your class in inner for Lab10 so it should be attached to the enclosing instance. Usually inner classes are created inside the members of the class.

It compiles after this:

Three randomized coefficients are: 
a = 5.0
b = 7.0
c = 3.0

QuadraticEquation is defined as an inner class within the class Lab10 . As such, it cannot be created except within an instance of the outer class, unless you change it to be a static inner class:

static class QuadraticEquation{
...
}

Or if you need it to be non-static, you can create an instance of the outer class when you create the inner class:

QuadraticEquation equation = new Lab10().new QuadraticEquation();

After a momentary puzzlement mirroring your own, I see that in your other situation, the class SimpleSquare is just not an inner class. It is another class at top level.

Java allows you to define multiple classes at the same level in the same file as long as only one of them is public.

The difference is the (possibly accidental) different placement of an end brace!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM