Results 1 to 8 of 8
  1. #1
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default Can't make static reference to non-static method -> huh?! Simple car prgm

    Hi fellow javaers. I am trying to write some simple code to practice my switch statements, scanning and general format skills. Basically inside of my class I have 2 methods, one of which is the main. I thought everything should work well but I think I have a fundamental error in the way I am doing this. The error I am receiving upon compilation is
    "Cannot make a static reference to the non-static method speedCalc(int,int) from the type Scanner_fun" on the line near the bottom of the main method where double vehSpeed calls the speedCalc method.

    and here is the code. Btw I am coding in eclipse.

    Java Code:
    import java.util.Scanner;
    
    public class Scanner_fun {
    	double gear1 = 3.321;
    	double gear2 = 1.902;
    	double gear3 = 1.308;
    	double gear4 = 1;
    	double gear5 = 0.759;
    	double gearR = 3;
    	double finalDrive = 4.083;
    	double wheelDia = 25; //inches
    	double inchesToMiles = 1/12/5280;
    	double wheelCirc = wheelDia*Math.PI*inchesToMiles;  //miles
    	
    	
    	private double speedCalc(int gear, int rpm){
    		double speed;
    		switch(gear){
    		case 0:
    			speed = rpm/gearR/finalDrive*wheelCirc;
    			return speed;
    		case 1:
    			speed = rpm/gear1/finalDrive*wheelCirc;
    			return speed;
    		case 2:
    			speed = rpm/gear2/finalDrive*wheelCirc;
    			return speed;
    		case 3:
    			speed = rpm/gear3/finalDrive*wheelCirc;
    			return speed;
    		case 4:
    			speed = rpm/gear4/finalDrive*wheelCirc;
    			return speed;
    		case 5:
    			speed = rpm/gear5/finalDrive*wheelCirc;
    			return speed;
    		}
    		return 0.0;
    	}
    	
    	public static void main (String[] args){
    		Scanner scan = new Scanner(System.in);
    		System.out.println("select gear (0 for R): ");
    		int gear = scan.nextInt();
    		System.out.println("select engine speed (rpm): ");
    		int rpm = scan.nextInt();
    		double vehSpeed = speedCalc(gear,rpm);
    		System.out.println("You are in gear: "+gear);
    		System.out.println("Your engine speed is: "+rpm);
    		System.out.println("Your vehicle speed is: "+vehSpeed);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    speedCalc is a non-static method and as such can only be called when associated with a Scanner_fun object. The solution is to either create a Scanner_fun object and call this method on the object, or make speedCalc static.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    For instance,


    Java Code:
    public class StaticTest {
       
       public void nonStaticMethod() {
          System.out.println("Non static method");
       }
       
       public static void staticMethod() {
          System.out.println("Static method");
       }
       
       public static void main(String[] args) {
          // static methods are called on the class:
          StaticTest.staticMethod();
          
          // but since we're inside the StaticTest class, the class name is not necessary:
          staticMethod();
          
          
          // non-static methods must be called on an appropriate object:
          StaticTest staticTestObject = new StaticTest();
          staticTestObject.nonStaticMethod();
       }
    }

    Clear as Mudd?

  4. #4
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    super clear, I understand how this is suppose to work now and will look a little more into what static and non-static mean, as this seems important and fundamental.

    It seems for this simple program the best route to go would be to make speedCalc a static method, but then eclipse alerts me to make all my vars (gear ratios, wheel diameter, etc) static as well. I'm guessing this is another must for this type of fix?

  5. #5
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    wow the last thing that tripped me out before my program ran was that
    double inchesToMile = 1/12/5280 gives me 0 but I guess that is a strong type langauge at work?

    Anyway just in case someone cares about the program logic this won't give the vehSpeed result in mph but miles / minute so engine speed will need *60 to give mph back.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Try this instead:
    double inchesToMile = 1.0/12.0/5280.0;

    The compiler assumes your decimal-less numbers are integers, and divides them as such.

  7. #7
    enerj is offline Member
    Join Date
    Sep 2010
    Posts
    30
    Rep Power
    0

    Default

    yep I changed just the 1 to 1.0 or 1d. I thought having the var declared as a double would auto type cast it but it didn't.

    When I scan for nextDouble(); however if I just put in 1 it will store it as a double. I did notice that this isn't allowed though;

    Java Code:
    int x = 5;
    x = (double)x; //- compile error - "Type mismatch: cannot convert from double to int"
    To me it looks like I am trying to convert from an int (x) to a double. Or maybe it says that because I am trying to store a double in a var that was declared as an int - which it is, because

    Java Code:
    double y = (double)x;
    does indeed work.

    I know I am blabbing on here but I think this could be useful to some other noobs trolling away.

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Your assumption is indeed correct. The problem was not with casting the int to a double but rather the storing of a double in an int variable.

Similar Threads

  1. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  2. Static method cannot make new objects?
    By zerkz in forum New To Java
    Replies: 2
    Last Post: 10-15-2009, 04:17 AM
  3. make static ref to non-static method?
    By McChill in forum New To Java
    Replies: 7
    Last Post: 02-23-2009, 06:48 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •