# Cannot call method because it is non-static

• November 7th, 2013, 11:14 AM
thegorila78
Cannot call method because it is non-static
I'm writing a program for my java class that has to solve quadratic equations. I have separate methods that calls the private variables a, b, and c. I also have separate methods for the discriminant and both the positive and negative roots. My code is as follows:
Code :

```import java.util.Scanner; public class QuadraticEquation { private double a; private double b; private double c; public static void main(String[] args){ Scanner numberInput = new Scanner(System.in); System.out.print("Enter a, b, c: "); double a = numberInput.nextDouble(); double b = numberInput.nextDouble(); double c = numberInput.nextDouble(); QuadraticEquation q1 = new QuadraticEquation(a, b, c); System.out.println(getRoot1());   }//end main method   public QuadraticEquation(double a, double b, double c){ this.a = a; this.b = b; this.c = c; }//end constructor   public double getDiscriminant(){   double discriminant = Math.sqrt((getB()*getB()) - (4*getA()*getC())); return discriminant;   }//end getDiscriminant()   public double getA(){ this.a = a; return this.a; }//end getA()   public double getB(){ this.b = b; return this.b; }//end getB()   public double getC(){ this.c = c; return this.c; }//end getC   public double getRoot1(){ double root1 = (-getB() + getDiscriminant()) / (2*a); return root1; }//end getRoot1()   public double getRoot2(){ double root2 = (-getB() - getDiscriminant()) / (2*a); return root2; }//end getRoot2() }```

My problem is that I get an error that says "Cannot make a static reference to the non-static method getRoot1() from the type QuadraticEquation". I know this means that it is not working because my method is not a static method but when I try to change it to a static method I get errors in all my other methods. I have no idea what to do any help would be appreciated.
• November 7th, 2013, 11:26 AM
aussiemcgr
Re: Cannot call method because it is non-static
I think you meant to invoke the getRoot1() method on your created QuadraticEquation q1 object. Correct?
When you have a main in a class and you attempt to call a method in the class directly, that method must also be static (because the main is static). However, if you invoke the method on an instance of the class (like your q1 object), the method does not need to be static.
Long story short, you want to change this:
Code java:

`System.out.println(getRoot1());`
To this:
Code java:

`System.out.println(q1.getRoot1());`

If you are not sure why that is the case, feel free to ask me to clarify on whatever you didn't understand.
• November 7th, 2013, 11:41 AM
thegorila78
Re: Cannot call method because it is non-static
First off I'd like to say thanks for the reply. I came to my senses about what you just posted before you posted it so I fixed that problem but now I have another one. In my assignment I have various conditions I have to meet, one of them being "if the discriminant < 0, output 'The Equation has no roots'". This is pretty straightforward but I'm not sure where I have to put that if statement. I'm guessing it has to be in both the getRoot methods but I have no idea which values to put into the statement.
• November 7th, 2013, 12:02 PM
aussiemcgr
Re: Cannot call method because it is non-static
You could put it in both if statements, or you could put the if statement in your main before you even call the getRoot methods. You can get the discriminant with your getter method: q1.getDiscriminant()
So: if discriminant < 0, print 'The Equation has no roots'; else, calculate and print the roots.
• November 7th, 2013, 12:19 PM
thegorila78
Re: Cannot call method because it is non-static
ok thanks a lot for the help man I appreciate it.