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: Using variables from different methods?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Using variables from different methods?

    Kindav a noobish question, but im having some troubles in several different programs with this. Im trying to use a variable that I defined in one method and use it in another method. To be a little specific, Im working on this (#1-pyramid), and Im trying to create one method that can find the center of the graphics window, and the next method that calls on one of the call on one of the variables when placing a block. If I wasn't making too much sense here, well heres what I got in the 5 minutes I worked on this before I got stuck:


    import acm.graphics.*;
    import acm.program.*;
    import java.awt.*;
     
    public class Pyramid extends GraphicsProgram {
     
    /** Width of each brick in pixels */
    	private static final int BRICK_WIDTH = 30;
     
    /** Width of each brick in pixels */
    	private static final int BRICK_HEIGHT = 12;
     
    /** Number of bricks in the base of the pyramid */
    	private static final int BRICKS_IN_BASE = 14;
     
    	public void run() {
    		findCenter();
    		startBase();
    	}
     
    	//This will find the center of the graphics window.
     
    	public void findCenter() {
    		int x = getWidth();
    		int y = getHeight();
    		int center = x/2;
    		int bottom = y;
    	}
     
    	private void startBase() {
     
    	}
     
    }


    I havent messed with graphics programs to much, so this is going to take some work for me to learn tomorrow. Thats also why I dont have anything else filled in after the startBase() method.


    Sry if some of this may not make sense. I should have dragged myself off to bed a while ago. But I guess if some of this did make sense, im looking forward to some answers!


  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: Using variables from different methods?

    If you want to make a local variable in one method available in another method, you must either pass it as a method parameter (you can have as many of these as you want0, or return it to the calling method (every method is limited to at most 1 of these).

    To declare what parameters a method should expect:

    public void someMethod(Type1 param1, Type2 param2) // etc. etc.
    {
    }

    Where Type1/2 is the type of the parameter variable, and param1/2 is the name you will assign to a new variable that will take on the value of the variable passed to the method.

    ex.:
    public void add(int a, int b)
    {
         System.out.println("a + b = " + (a+b));
    }

    In returning a value, you only need to specify the type that is being returned. You replace the "void" with the type you want. Void is just considered the absence of a type (aka. nothing is returned). Once you declare your method to return a certain type, it MUST return some valid value of that type in all possible branches of execution the program can take.

    public int add(int a, int b)
    {
         return a + b;
    }

    In your case, there are a few possible things you can do because you have 2 values that need to be returned.
    Choice 1: Create two methods, one that returns the center X coordinate, and the other that returns the bottom Y coordinate.
    int centerX = findCenterX();
    int centerY = findBottom();
    public int findCenterX()
    {
         int x = getWidth();
         return x / 2;
    }
    public int findBottomY()
    {
          return getHeight();
    }
    Choice 2: Create an object that holds both values (or use the Dimension class, doesn't matter), then return that one object.
    public class Dim
    {
         // ignore my bad example by declaring these public :P
         public int x, y;
         public Dim(int x, int y)
         {
              this.x = x;
              this.y = y;
         }
    }
    public Dim findCenter()
    {
    	int x = getWidth();
    	int y = getHeight();
    	int center = x/2;
    	int bottom = y;
    	return new Dim(center, bottom);
    }

    Choice 3: Have either a class instance or static variable.
    public class Pyramid extends GraphicsProgram
    {
         int centerX, bottomY;
     
         public void findCenter()
         {
              this.centerX = getWidth() / 2;
              this.bottomY = getHeight();
         }
    }

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

    Default Re: Using variables from different methods?

    Ok so I went with choice 2, but how exactly am I supposed to stick those in the coordinates for a new GRect?

    public void startBase() {
    		GRect base1 = new GRect(x, y, 12, 30);
    	}

    I stuck x and y in there ("the field component.x is not visible--- the field component.y is not visible) , as well as center, bottom ("could not be resolved"). Neither of those seem to work and given that I havent learned too much about dimensions and some of that other code you had Im sortav at a loss for how this would work...

    Sorry if Im seeming like I dont have a clue about allot of this stuff, but thats why Im here.

  4. #4
    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: Using variables from different methods?

    Remember, you must pass the arguments to the function. It is perfectly legal to pass any type of parameter to a method so long as that method is declared to accept that type of object (see above examples for how to do this).

    A second item you'll need to take into account is because you do have a Dim object, you must reference x and y from that object. This is done using the "." operator (dot operator).

    Dim someDimension = new Dim(1,0);
    System.out.println("The center x coordinate is " + someDimension.x);
    System.out.println("The center y coordinate is " + someDimension.y);

Similar Threads

  1. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 01:07 PM
  2. Storing in different types of variables
    By giorgos in forum Collections and Generics
    Replies: 0
    Last Post: November 22nd, 2009, 02:02 PM
  3. how to extract variables,keywords,operator...
    By Nanda in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2009, 10:19 PM
  4. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 5th, 2009, 12:57 AM
  5. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 09:07 AM