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

Thread: Do method variables get reset each time a method is used?

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    26
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Do method variables get reset each time a method is used?

    The following code is part of a method that's used in a program I'm doing. The method is called by a loop so it is used multiple times. As the variables values are declared at the top of the method, my question is if these values all return to zero each time the method is called by my loop?

     // user defined method
             static int product(int num1, int num2){      // declare product method and parameters to receive arguments
     
                int i = 0, r = 0;                         // "i" variable controls "sum" index _ "r" holds remainder
                int[] sum = new int[20];
                int product = 0;                          // return value of multiplication
     
                while(num1 != 0 || num2 !=0){             // while there are digits remaining in either number input

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Do method variables get reset each time a method is used?

    Yes, variables declared within a method are recreated each time the method is called. Their values are gone when the method exits.
    If you don't understand my answer, don't ignore it, ask a question.

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

    FightingIrishman (January 1st, 2022)

Similar Threads

  1. [SOLVED] JNI CallStaticVoidMethod crash when Java variables declared outside method
    By SGARCH in forum Java Native Interface
    Replies: 2
    Last Post: November 5th, 2013, 09:49 AM
  2. Using Date() to get Start Time and Finish Time of a copyFiles method
    By dalythe in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 17th, 2013, 09:50 PM
  3. [SOLVED] how to trace recursion variables in a recursive method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 12
    Last Post: May 23rd, 2012, 12:00 PM
  4. Problems With Method Calls and Variables
    By connor3452 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2012, 05:46 AM
  5. Why I can't return two variables in one method?
    By netlync in forum Java Theory & Questions
    Replies: 3
    Last Post: January 13th, 2012, 02:39 AM