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

Thread: Using variable across methods

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Using variable across methods

    Hi, I'm looking to use the variable flagDimensions in method norwayFlag after defining it as shown. However, it's telling me that the variable is undefined. Please ignore the UI. etc, it's part of a custom library used by the learning institution. Please also note that this is not the whole code, just the relevant piece.
    So, what have I done wrong?! Thanks!

        public void flagDimensions(double x, double y, double flagHt, double flagLgt){
            x = 100;
            y = 100;
            flagLgt = (flagHt/2)*3;
        }
     
        /** The flag for Norway is a red rectangle with
         * a blue cross with a white border, slightly off-set to the left-hand side;
         */
        public void norwayFlag(){
            UI.initialise();      // not necessary, but avoids some problems when debugging
            UI.setColor(Color.red);
            UI.fillRect(flagDimensions);
            UI.setColor(Color.white);
            UI.fillRect(x-((flagHt/5)*2), y, (flagHt/5), flagLgt);
        }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Using variable across methods

    I'm looking to use the variable flagDimensions in method norwayFlag after defining it as shown. However, it's telling me that the variable is undefined.
    Yes the variable flagDimensions is undefined. What you defined was a method flagDimensions().

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using variable across methods

    Oh, ok, my mistake. Can you please inform me of how to correct said mistake, and direct my program along the proper path?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Using variable across methods

    It's a bit difficult to say *how* you should do whatever it is you want to do without knowing first what it *is* that you want to do.

    Currently the flagDimensions() method does nothing at all. It assigns some values to the parameter variables, but these variables last only as long as the method does. Ie they will "go out of scope" - disappear without trace - after the method finishes, so assigning anything to them is without any lasting effect.

    As far as the fillRect() method is concerned, look in the documentation for that class and see what sort of arguments it expects you to pass it. (The number and type of those arguments)

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

    willpope (March 16th, 2012)

  6. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using variable across methods

    Thank you for your assistance.

  7. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Using variable across methods

    You're welcome.

Similar Threads

  1. Replies: 5
    Last Post: November 16th, 2011, 10:22 AM
  2. non-static variable
    By frozen java in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 15th, 2011, 06:18 PM
  3. [SOLVED] what is this? Variable?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 7th, 2011, 11:32 PM
  4. How to use the same variable in various JPanel?
    By danielpereira in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 19th, 2010, 12:08 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM

Tags for this Thread