Results 1 to 3 of 3
  1. #1
    cagipple is offline Member
    Join Date
    Mar 2011
    Posts
    11
    Rep Power
    0

    Default using variables in multiple methods

    I have three methods:

    method 1 gets an integer from the user and stores it in int x.

    method 2 gets another integer from the user and stores it in int y.

    method 3 performs the equation x * y and returns the value.

    How can I get the values of x and y into method 3 so it can do the equation?

  2. #2
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: using variables in multiple methods

    I believe you would have to create a separate method for x and y then call that method x,y() in the main method

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: using variables in multiple methods

    You can do this two ways, either have the methods return an int,
    Java Code:
    System.out.println(method3(method1(), method2()));
    Or you can have instance variables which the first two methods set, and the third method creates.

Similar Threads

  1. Static variables and methods
    By sudharani in forum New To Java
    Replies: 5
    Last Post: 04-25-2011, 02:07 PM
  2. Using variables inside methods?
    By Forty0ztoFreedom in forum New To Java
    Replies: 2
    Last Post: 04-08-2011, 07:36 PM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 04:14 PM
  5. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 09:52 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
  •