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 9 of 9

Thread: Cannot make static reference to a non-static method?

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot make static reference to a non-static method?

    Hi everyone, this is my first post. I'm trying to implement a case-break menu into a class. Here's the code:

    import java.util.Scanner;
     
     
    public class UnitsToCups {
     
    	public void displayMenu(){
    		System.out.println("Option 1: Convert teaspoons to cups\n Option 2: Convert tablespoons to cups\n" +
    							"Option 3: Convert pints to cups\n Option 4: Convert quarts to cups\n Option 5: Convert gallons to cups\n");
    	}
    	double tsp = 0.0208333333333333;//1 teaspoon = 1/48 of a cup
    	double tbsp = 0.0625;//1 tablespoon = 1/16 of a cup
    	int pint = 2;//1 pint = 2 cups
    	int quart = 4;//1 quart = 4 cups
    	int gallon = 16;//1 gallon = 16 cups
     
    	public void Menu(){
    		Scanner menuSelect = new Scanner(System.in);
    		displayMenu();
    		switch(menuSelect.nextInt()){
    		case 1:
    			System.out.println("Enter number of teaspoons: ");
    			Scanner teaspoons = new Scanner(System.in);
    			int numTsp = teaspoons.nextInt();
    			double cups1 = tsp * 1;
    			System.out.println(numTsp + " teaspoons is equivalent to " + cups1 + " Cups, or (Fraction?)");
    			teaspoons.close();
    			break;
    		case 2:
    			System.out.println("Enter number of tablespoons: ");
    			Scanner tablespoons = new Scanner(System.in);
    			int numTbsp = tablespoons.nextInt();
    			double cups2 = tbsp * 1;
    			System.out.println(numTbsp + " tablespoons is equivalent to " + cups2 + " Cups, or (Fraction?)");
    			tablespoons.close();
    			break;
    		case 3:
    			System.out.println("Enter number of pints: ");
    			Scanner pints = new Scanner(System.in);
    			int numPints = pints.nextInt();
    			double cups3 = pint * 1;
    			System.out.println(numPints + " pints is equivalent to " + cups3 + " Cups, or (Fraction?)");
    			pints.close();
    			break;
    		case 4:
    			System.out.println("Enter number of quarts: ");
    			Scanner quarts = new Scanner(System.in);
    			int numQuarts = quarts.nextInt();
    			double cups4 = quart * 1;
    			System.out.println(numQuarts + " quarts is equivalent to " + cups4 + " Cups, or (Fraction?)");
    			quarts.close();
    			break;
    		case 5:
    			System.out.println("Enter number of gallons: ");
    			Scanner gallons = new Scanner(System.in);
    			int numGallons = gallons.nextInt();
    			double cups5 = gallon * 1;
    			System.out.println(numGallons + " gallons is equivalent to " + cups5 + " Cups, or (Fraction?)");
    			gallons.close();
    			break;
    		default:
    			System.err.println("Invalid option. Choose an option from 1 to 5.");
    			break;
    		menuSelect.close();
    		}
    	}	
     
    	public static void main(String[] args){
    		displayMenu();
    		Menu();
    	}
    }

    The issues are:

    Line 63: "Unreachable code"

    Line 68: "Cannot make a static reference to the non-static method displayMenu() from the type UnitsToCups"

    Line 69: "Cannot make a static reference to the non-static method Menu() from the type UnitsToCups"

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot make static reference to a non-static method?

    Cannot make a static reference to the non-static method
    This is because the calls are being made from the static method: main. The non static methods can only be referenced when a reference to an instance of the class is used.
    Create an instance of the class and use the reference to that instance to call the class.

    Here is an example from your code:
    		Scanner gallons = new Scanner(System.in);    // create instance of class with reference: gallons
    		int numGallons = gallons.nextInt();                // use reference to call class's nextInt method

    Another (non-OOP) solution is to make the methods static.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    167
    My Mood
    Cool
    Thanks
    0
    Thanked 21 Times in 21 Posts

    Default Re: Cannot make static reference to a non-static method?

    Unreachable code means that due to some logic, that code won't be reached. Here is an example:


    void foo() {
        // do something
        return;
        a = 10;  <-- this code will not be reached.
    }

    In your case, it was menuSelect.close() after the break. But just delete the statement, don't close Scanner.


    Regarding the static reference. That means you can't access an instance field or method from a static context.

    class Foo {
        int a; // instance field
        public static void main(String[] args) {  <-- note the word static.
                a = 10; <--  not allowed.  You would have to do it like this
                Foo f = new Foo();
                f.a = 10;
        }
    }

    Also, only create one instance of Scanner. If you use it to read input from the console, don't close it.

    Regards,
    Jim

  4. #4
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot make static reference to a non-static method?

    Quote Originally Posted by jim829 View Post
    Also, only create one instance of Scanner. If you use it to read input from the console, don't close it.
    But won't my program crash or be stuck in limbo if I don't close the menu?

    Also, I have a new logical problem. It involves lines 11, 24, and 25 of the following code:
    import java.util.Scanner;
     
     
    public class UnitsToCups {
     
    	public void displayMenu(){
    		System.out.println("Option 1: Convert teaspoons to cups\n Option 2: Convert tablespoons to cups\n" +
    							"Option 3: Convert pints to cups\n Option 4: Convert quarts to cups\n Option 5: Convert gallons to cups\n" +
    							"Option 6: Exit");
    	}
    	double tsp = 1/48;//0.0208333333333333;//1 teaspoon = 1/48 of a cup
    	double tbsp = 0.0625;//1 tablespoon = 1/16 of a cup
    	int pint = 2;//1 pint = 2 cups
    	int quart = 4;//1 quart = 4 cups
    	int gallon = 16;//1 gallon = 16 cups
     
    	public void Menu(){
    		Scanner menuSelect = new Scanner(System.in);
    		//while(menuSelect < 6){
    			switch(menuSelect.nextInt()){
    			case 1:
    				System.out.println("Enter number of teaspoons: ");
    				Scanner teaspoons = new Scanner(System.in);
    				int numTsp = teaspoons.nextInt();
    				double cups1 = tsp * numTsp;
    				System.out.println(numTsp + " teaspoons is equivalent to " + cups1 + " Cups, or (Fraction?)");
    				teaspoons.close();
    				break;
    			case 2:
    				System.out.println("Enter number of tablespoons: ");
    				Scanner tablespoons = new Scanner(System.in);
    				int numTbsp = tablespoons.nextInt();
    				double cups2 = tbsp * 1.0;
    				System.out.println(numTbsp + " tablespoons is equivalent to " + cups2 + " Cups, or (Fraction?)");
    				tablespoons.close();
    				break;
    			case 3:
    				System.out.println("Enter number of pints: ");
    				Scanner pints = new Scanner(System.in);
    				int numPints = pints.nextInt();
    				double cups3 = pint * 1.0;
    				System.out.println(numPints + " pints is equivalent to " + cups3 + " Cups, or (Fraction?)");
    				pints.close();
    				break;
    			case 4:
    				System.out.println("Enter number of quarts: ");
    				Scanner quarts = new Scanner(System.in);
    				int numQuarts = quarts.nextInt();
    				double cups4 = quart * 1.0;
    				System.out.println(numQuarts + " quarts is equivalent to " + cups4 + " Cups, or (Fraction?)");
    				quarts.close();
    				break;
    			case 5:
    				System.out.println("Enter number of gallons: ");
    				Scanner gallons = new Scanner(System.in);
    				int numGallons = gallons.nextInt();
    				double cups5 = gallon * 1.0;
    				System.out.println(numGallons + " gallons is equivalent to " + cups5 + " Cups, or (Fraction?)");
    				gallons.close();
    				break;
    			case 6:
    				System.out.println("Bye!");
    				menuSelect.close();
    				break;
    			default:
    				System.err.println("Invalid option. Choose an option from 1 to 5.");
    				//break;			
    			}			
    		//}//end of while loop
     
    	}	
     
    	public static void main(String[] args){
    		UnitsToCups x = new UnitsToCups();
    		x.displayMenu();
    		System.out.println();
    		x.Menu();		
    	}
    }

    We know that because of the double variable declaration on line 11, tsp is assigned to a value of 0.0208333333333333. However, because tsp is less than 1, multiplying it by any integer (as we do on line 25) will result in 0, because of the 0 on the left side of the decimal point makes it so. This is evident in the console output of my most recent program test:

    DoubleReadasAzero.PNG

    What's the best approach of fixing this? Do I need to convert numTsp from line 24 into a double before multiplying it as I currently do on line 25?

    One other thing. Is the nature of a case/break menu structure to always quit after the user selects a choice, or is it possible to keep a case/break menu going (with a while loop or something) until the user selects option 6?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot make static reference to a non-static method?

    double tsp =  1/48;
    The value of that expression will be 0. To get a double value change one of the operands to a double: 1/48.0

    nature of a case/break menu structure
    The break will exit the switch statement instead of falling through to the next case statement.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    167
    My Mood
    Cool
    Thanks
    0
    Thanked 21 Times in 21 Posts

    Default Re: Cannot make static reference to a non-static method?

    But won't my program crash or be stuck in limbo if I don't close the menu?
    No, it won't. In fact, if you were to continue to ask for input for conversions, say in a loop, your program wouldn't work.

    Try running this simple program to see what happens when you close the Scanner and try to reopen it and take input.

    import java.util.Scanner;
     
    public class ScannerDemo {
       public static void main(String[] args) {
    	  Scanner input = new Scanner(System.in);
    	  System.out.print("Enter a number: ");
    	  int v = input.nextInt();
    	  System.out.println("You entered " + v);
    	  input.close();
    	  input = new Scanner(System.in);
    	  System.out.print("Enter a number: ");
    	  v = input.nextInt(); // <--- KaBoom!!
    	  System.out.println("You entered " + v);
    	  System.out.printf("Done!");
       }
    }

    The reason this happens is when you close the Scanner, it then closes the console for input.

    So a single instance of Scanner should be used for all of your console input. And don't close it.

    Regards,
    Jim

  7. #7
    Junior Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cannot make static reference to a non-static method?

    Quote Originally Posted by jim829 View Post
    The reason this happens is when you close the Scanner, it then closes the console for input.

    So a single instance of Scanner should be used for all of your console input. And don't close it.
    Is there any way that I could get my program to repeat the switch structure until the user chooses 6?

    import java.util.Scanner;
     
     
    public class UnitsToCups {
     
    	public void displayMenu(){
    		System.out.println("Option 1: Convert teaspoons to cups\n Option 2: Convert tablespoons to cups\n" +
    							"Option 3: Convert pints to cups\n Option 4: Convert quarts to cups\n Option 5: Convert gallons to cups\n" +
    							"Option 6: Exit");
    	}
    	double tsp = 1/48.0;//0.0208333333333333;//1 teaspoon = 1/48 of a cup
    	double tbsp = 0.0625;//1 tablespoon = 1/16 of a cup
    	int pint = 2;//1 pint = 2 cups
    	int quart = 4;//1 quart = 4 cups
    	int gallon = 16;//1 gallon = 16 cups
     
    	public void Menu(){
    		Scanner menuSelect = new Scanner(System.in);
    		//while(menuSelect < 6){
    			switch(menuSelect.nextInt()){
    			case 1:
    				System.out.println("Enter number of teaspoons: ");
    				Scanner teaspoons = new Scanner(System.in);
    				int numTsp = teaspoons.nextInt();
    				double cups1 = tsp * numTsp;
    				System.out.println(numTsp + " teaspoons is equivalent to " + cups1 + " Cups, or (Fraction?)");
    				teaspoons.close();
    				break;
    			case 2:
    				System.out.println("Enter number of tablespoons: ");
    				Scanner tablespoons = new Scanner(System.in);
    				int numTbsp = tablespoons.nextInt();
    				double cups2 = tbsp * numTbsp;
    				System.out.println(numTbsp + " tablespoons is equivalent to " + cups2 + " Cups, or (Fraction?)");
    				tablespoons.close();
    				break;
    			case 3:
    				System.out.println("Enter number of pints: ");
    				Scanner pints = new Scanner(System.in);
    				int numPints = pints.nextInt();
    				double cups3 = pint * numPints;
    				System.out.println(numPints + " pints is equivalent to " + cups3 + " Cups, or (Fraction?)");
    				pints.close();
    				break;
    			case 4:
    				System.out.println("Enter number of quarts: ");
    				Scanner quarts = new Scanner(System.in);
    				int numQuarts = quarts.nextInt();
    				double cups4 = quart * numQuarts;
    				System.out.println(numQuarts + " quarts is equivalent to " + cups4 + " Cups, or (Fraction?)");
    				quarts.close();
    				break;
    			case 5:
    				System.out.println("Enter number of gallons: ");
    				Scanner gallons = new Scanner(System.in);
    				int numGallons = gallons.nextInt();
    				double cups5 = gallon * numGallons;
    				System.out.println(numGallons + " gallons is equivalent to " + cups5 + " Cups, or (Fraction?)");
    				gallons.close();
    				break;
    			case 6:
    				System.out.println("Bye!");
    				//menuSelect.close();//A single instance of scanner should be used for all console input, so don't close it.
    				break;
    			default:
    				System.err.println("Invalid option. Choose an option from 1 to 5.");
    				break;			
    			}
    			System.out.println("The program has passed the default portion of the switch structure.");
    		//}//end of while loop
     
    	}	
     
    	public static void main(String[] args){
    		UnitsToCups x = new UnitsToCups();
    		x.displayMenu();
    		System.out.println();
    		x.Menu();		
    	}
    }

    PS: how do I remove attached thumbnails from my posts?
    Attached Images Attached Images
    Last edited by SamJava_the_Hut; December 6th, 2018 at 01:33 AM. Reason: I just figured out that commenting out the breaks caused my program to crash.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: Cannot make static reference to a non-static method?

    get my program to repeat the switch structure until the user chooses 6?
    Yes, use a loop.
    What happened when the while was working?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    167
    My Mood
    Cool
    Thanks
    0
    Thanked 21 Times in 21 Posts

    Default Re: Cannot make static reference to a non-static method?

    BTW, Your thumbnail image of the error "NoSuchElementException" is what happens when you try to read from a closed console.

    Regards,
    Jim

Similar Threads

  1. Replies: 6
    Last Post: May 3rd, 2013, 05:25 PM
  2. Replies: 1
    Last Post: April 3rd, 2012, 07:32 AM
  3. Replies: 1
    Last Post: February 14th, 2012, 07:42 AM
  4. Replies: 9
    Last Post: November 5th, 2011, 11:22 AM
  5. Replies: 10
    Last Post: September 6th, 2010, 05:48 PM