Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Cannot call method because it is non-static

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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:
    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.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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:
    System.out.println(getRoot1());
    To this:
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default 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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    thegorila78 (November 7th, 2013)

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    My Mood
    Confused
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Cannot call method because it is non-static

    ok thanks a lot for the help man I appreciate it.

Similar Threads

  1. How to call a static method within a static method in the same class?
    By EDale in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 04:13 AM
  2. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  3. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  4. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM