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: Need help understanding method calls and such

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help understanding method calls and such

    Hi,

    First and foremost, this is not homework (believe it or not some forums freak out about this, so wanted to get this out of the way). I took a Java course in college and want to get back into it. I can do some simple stuff, but am more interested in the basis of object oriented programming (in other words not doing everything in the main method).

    Here is the code I'm working with:
    import javax.swing.*;
     
    public class Experiment
    {
     
    	public static void main(String [] args)
    	{
     
    		String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
    		String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
     
    		Double getnumber1 = Double.parseDouble(number1);
    		Double getnumber2 = Double.parseDouble(number2);
     
    		JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
     
    		//JOptionPane.showMessageDialog(null, "The numbers added together are" + addNumbers() );
     
    	}
     
     
    	/*
    	public double addNumbers()
    	{
     
    		double total;
     
    		total = getnumber1 + getnumber2;
     
    		return total;
     
    	}
    	*/
     
    }


    What I'm doing
    I'm using the JOptionPane simple dialog boxes to try and add together some numbers generated by users. I like JOptionPane, so please don't suggest using Scanner. What I'm trying to do is have the addNumbers part return up to the last JOptionPane message which will show the user the total. I commented out the parts that don't work, the last part of the program and the last JOptionPane message dialog.

    What I want to do (ideally) is do all the calculations out of the main method and then be able to call them when needed. So the last part of the code may be totally incorrect (just the commented out part) and the last JOptionPane (also commented out).

    So can someone explain to me in simple terms the syntax for calling other methods and such? If I'm totally off base and am doing everything completely wrong, please let me know as well.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help understanding method calls and such

    the parts that don't work
    What didn't work? If there are errors, don't ignore them, copy and paste them here.

    The error is probably about using non-static methods. Your short and simple program doesn't use classes for your tests so you should make all your methods static. Later when you start using classes, you'll want to remove all the static modifiers, except for main()s.

    The other problem has to do with the scope of some of your variables. They are ONLY know inside of the main() method. For other methods to see them, they must be defined outside of any method. Again you'll need the static modifier.

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding method calls and such

    Sorry, I didn't think the error would be useful because I figured I was doing it totally wrong.

    You are right about the error, "non-static method addNumbers() cannot be referenced from a static context".

    So if I move the getnumber variables out of the main method, I get:

    public class Experiment
    {
     
    	double getnumber1, getnumber2;
     
    	public static void main(String [] args)
    	{
     
    		String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
    		String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
     
    		Double getnumber1 = Double.parseDouble(number1);
    		Double getnumber2 = Double.parseDouble(number2);
     
    		JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
     
    		JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers() );
     
    	}
     
    	public double addNumbers()
    	{
     
    		double total;
     
    		total = getnumber1 + getnumber2;
     
    		return total;
     
    	}
     
    }

    So is there an easy way to get around that problem without rewriting the entire simple program? To be honest, I figured the very last chunk of code (starting with public double) was incorrect.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help understanding method calls and such

    You now have two definitions for getnumber1 and getnumber2. The ones in the main() method will get values, the ones outside will not.

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

    Default Re: Need help understanding method calls and such

    Norm is not entirely correct, but hes partly there. It is true that you have two definitions, but lets actually look at the error.

    You are both correct and incorrect.
    1st issue is that addNumbers needs to be declared static. This is because you are calling it in the main.
    2nd is that the getnumber1 and getnumber2 variables ALSO need to be declared static, because these are being references both in the static main and the static addNumbers method. There is an alternative to setting the variables static. You can also send them with the method call. Alternatively yet again, you can create the addNumbers method inside the main.
    3rd issue is that when you declared the variables getnumber1 and getnumber2 in the main, you declared them Double and not double. The difference? Double is the Double Object. double is the double simple-type. The Double.parseDouble(String) method returns a double (simple-type).

    Here is the code for all 3 examples.

    Declaring both method and variables static.
    public class Experiment
    {
     
    	static double getnumber1, getnumber2;
     
    	public static void main(String [] args)
    	{
     
    		String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
    		String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
     
    		getnumber1 = Double.parseDouble(number1);
    		getnumber2 = Double.parseDouble(number2);
     
    		JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
     
    		JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers() );
     
    	}
     
    	public static double addNumbers()
    	{
     
    		double total;
     
    		total = getnumber1 + getnumber2;
     
    		return total;
     
    	}
     
    }

    Sending variables with method
    public class Experiment
    {
     
    	public static void main(String [] args)
    	{
     
    		String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
    		String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
     
    		double getnumber1 = Double.parseDouble(number1);
    		double getnumber2 = Double.parseDouble(number2);
     
    		JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
     
    		JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers(getNumber1,getNumber2) );
     
    	}
     
    	public static double addNumbers(double v1, double v2)
    	{
     
    		double total;
     
    		total = v1+ v2;
     
    		return total;
     
    	}
     
    }

    Creating method in main
    public class Experiment
    {
     
    	public static void main(String [] args)
    	{
     
    		String number1 = JOptionPane.showInputDialog(null, "Please enter your first number");
    		String number2 = JOptionPane.showInputDialog(null, "Please enter your second number");
     
    		double getnumber1 = Double.parseDouble(number1);
    		double getnumber2 = Double.parseDouble(number2);
     
    		JOptionPane.showMessageDialog(null, "The number's are " + getnumber1 + "and" + getnumber2);
     
    		JOptionPane.showMessageDialog(null, "The numbers added togethere are" + addNumbers() );
     
     
     
    	        public double addNumbers()
    	        {
     
    		      double total;
     
    		      total = getnumber1 + getnumber2;
     
    		      return total;
     
    	        }
            }
    }


    Also, at the risk of getting smeared, JOptionPane kicks the crap out of Scanner.

    Even so, I always prefer making my own GUIs.

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding method calls and such

    Thanks for the explanations.

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

    Default Re: Need help understanding method calls and such

    The third one is incorrect. I cant remember how to properly create methods in the main. The other 2 are correct though.

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

    Default Re: Need help understanding method calls and such

    You aren't allowed to create methods inside of other methods (as far as I know).

    You can create anonymous classes, though and these can have methods inside of them.

    public class Test
    {
        public static void main(String[] args)
        {
            Runnable run = new Runnable()
    		{
    			@Override
    			public void run()
    			{
    				System.out.println("Hello world!");
    			}
    		};
            new Thread(run).start();
        }
    }

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

    Default Re: Need help understanding method calls and such

    Thats probably what I was thinking of.

Similar Threads

  1. Dare 2010 calls for emerging game developers
    By jeet893 in forum Java Networking
    Replies: 0
    Last Post: March 5th, 2010, 03:51 PM
  2. Need help understanding GUI screen layouts and labels
    By Bill_H in forum AWT / Java Swing
    Replies: 3
    Last Post: December 2nd, 2009, 11:50 PM
  3. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM
  4. Any way to map method calls?
    By Swiftslide in forum Collections and Generics
    Replies: 1
    Last Post: September 21st, 2009, 04:37 AM