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

Thread: Non-Static variables

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Location
    Manchester
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Non-Static variables

    Right, I'm doing some university work, and I've got this message a few times but it just isn't going away with this program. Can someone please help with the static and non-static referencing for variables please? I have looked at the other posts in reference to this problem and it's still not helping with this piece of code
    import java.util.*;
     
    public class temperatureConverterMenu
    {
    	public Scanner sc = new Scanner(System.in);
     
    	char choice;
    	double centigrade, farenheit;
     
    	public void menu()
    	{
    		Scanner sc = new Scanner(System.in);
     
    		System.out.println("Please choose from the following menu...");
    		System.out.println("To enter a centigrade value to convert to farenheit enter 1");
    		System.out.println("To enter a farenheit value to convert to centigrade enter 2");
    		System.out.println("Please enter -1 to quit the program");	
    		System.out.println("------------- * -------------");
     
    		choice = sc.next().charAt(0);
     
    		do{
     
    			switch (choice)
    			{
    				case '1':
    				{
    					System.out.println("You have chosen centigrade to farenheit");
    					System.out.println("Please enter your centigrade value:");
     
    					centigrade = sc.nextDouble();
    					centigradeToFarenheit(centigrade);
    					break;
    				}
     
    				case '2':
    				{
    					System.out.println("You have chosen farenheit to centigrade");
    					System.out.println("Please enter your farenheit value:");
     
    					farenheit = sc.nextDouble();
    					farenheitToCentigrade(farenheit);
    					break;
    				}
     
    				default:
    					System.out.println("You did not enter a menu option...");
    					break;
    			}
     
    		} while(choice != -1);
     
    	}
     
    	// centigrade to farenheit
    	public void centigradeToFarenheit(double centigrade)
    	{
    		double Faren;
     
    		Faren = (centigrade*9)/5;
    		System.out.println(centigrade+"C is "+Faren+"F");
     
    		//return Faren;
    	}
     
    	//farenheit to centigrade
    	public void farenheitToCentigrade(double farenheit)
    	{
    		double Cent;
     
    		Cent = (farenheit*5)/9;
    		System.out.println(farenheit+"F is "+Cent+"C");	
     
    		//return Cent;
    	}
     
    	public static void main(String[] args) throws java.io.IOException
    	{
    		System.out.println("Welcome to the temperature converter");
     
    		menu();
    	}
    }

    Thank you
    Last edited by helloworld922; December 30th, 2010 at 01:00 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Non-Static variables

    In the future, please surround your code with highlight tags.

    See: Common Java mistakes - Referencing Non-static variables/methods in a static way

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    liloka (December 30th, 2010)

  4. #3
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Non-Static variables

    instantiate a new temperature object in your main(), and it should work.
        public static void main(String[] args) throws java.io.IOException
        {
            System.out.println("Welcome to the temperature converter");
            temperatureConverterMenu t = new temperatureConverterMenu();
            t.menu();
        }

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

    JavaPF (December 31st, 2010)

  6. #4
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Non-Static variables

    u simply have to make instance of your class and use that class instance dot this method like..

    temperatureConverterMenu temp = new temperatureConverterMenu();
    temp.menu();

Similar Threads

  1. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 06:51 PM
  2. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  3. How to use static imports
    By helloworld922 in forum Java Programming Tutorials
    Replies: 0
    Last Post: July 3rd, 2010, 11:22 AM
  4. How do I set a static variable??
    By wingchunjohn in forum Object Oriented Programming
    Replies: 4
    Last Post: January 22nd, 2010, 03:36 AM
  5. Static to non-static - Organization
    By copeg in forum Object Oriented Programming
    Replies: 5
    Last Post: December 22nd, 2009, 12:56 PM

Tags for this Thread