0

I am having heartburn with understanding java's variable scoping. Particularly with variables that are scoped for the entire class and being used in methods inside that class, yet not being able to retain their values after leaving the method. I don't get the point.

If I scope a variable inside a method, I expect it to not be available outside the method. But scoped in a class, used in a method in that class, and still the variable's value is *not accessible outside the method? After 6 hours trying to figure out less than 40 lines of code, it's got to be something obvious, but I'm clearly not smart enough... What concept am I not understanding about scope in java?

package myExperimentTests;

import myExperiment.DataUtilities;

public class MyExperimentTests {
public static String num = "2223334444";
public static String ocn = "4134";
public static String company = "This and That, Inc.";
public static long iterations = 1000000;
public static long startTime, stopTime;
public static long accumalatedTime;

public static void main(String[] args) {

    DataUtilities utils = new DataUtilities();

    for (int i=0; i<iterations; i++){

        startTime = System.currentTimeMillis();
        utils.IsPhoneNumberChar(num.toCharArray(), 10, 10);
        stopTime = System.currentTimeMillis();      
        accumalatedTime += stopTime - startTime;
    }       
    System.out.println("Evaluating " + num + " as array of char 1Mil times: " + accumalatedTime/iterations);

    for (int i=0; i<iterations; i++){
        startTime = System.currentTimeMillis();
        utils.IsPhoneNumberString(num, 10, 10);
        stopTime = System.currentTimeMillis();      
        accumalatedTime += stopTime - startTime;
    }
    System.out.println("Evaluating " + num + " as string 1Mil times: " + accumalatedTime/iterations);
}
}

I'm not sure why i'm being asked to explain why a beginner would know the suggested answer would be the same as the answer to this. My inexperience led me to believe this was a variable scope problem, not a datatype/division problem.

EricO
  • 49
  • 8
  • Are these variables inside the `DataUtilities` class? If so, please include that code. I'm guessing you are declaring the variables incorrectly for what you are trying to do. – forgivenson Oct 19 '16 at 18:34
  • 1
    You only have one method, can you include the code of the method which where the variable is "not accessible outside the method?" – Peter Lawrey Oct 19 '16 at 18:35
  • 2
    Scoping is very simple in Java, a variable is available anywhere inside the `{` `}` they are defined immediately inside and anywhere you can get a reference to them. – Peter Lawrey Oct 19 '16 at 18:36
  • 1
    Given that code, what's the actual problem? – Mad Physicist Oct 19 '16 at 18:45
  • Are you using the `static` keyword with a good understanding of what it does? And are you calling `main` multiple times, i.e. rerunning the application and trying to get the updated values after rerunning? – Lucas Ross Oct 19 '16 at 18:46
  • 1
    In the second loop, you're setting accumalatedTime (btw correct spelling is accumulated fwiw) to zero at each iteration of the loop. You sure that's what you intended? – Steve Harrington Oct 19 '16 at 19:44
  • @SteveHarrington That was a remnant of one of the many attempts to do this. I edited to remove that, but the issue remains. And thanks for pointing to the spelling mistake. I think. ;) – EricO Oct 19 '16 at 20:33
  • @forgivenson the variables are declared in the code provided. when the println statement runs, the accumulatedTime is always 0 – EricO Oct 19 '16 at 20:34
  • @PeterLawrey it's the accumalatedTime (yes, i know it's misspelled :) in the println statement – EricO Oct 19 '16 at 20:37
  • @MadPhysicist when I try to println accumalatedTime it is always 0, even though I can see it increment while debugging, but as soon as it leave the for loop, it's 0. – EricO Oct 19 '16 at 20:39
  • @LucasRoss Nope. The point of the exercise is to learn. That said, the variables are declared in the class scope where they are exclusively used, and no copies of the class are being created. – EricO Oct 19 '16 at 20:45
  • @EricO the accumulated time is the same as the time from end to end. It's in scope in main so I don't understand the problem. – Peter Lawrey Oct 19 '16 at 20:53
  • Possible duplicate of [Why does the division of two integers return 0.0 in Java?](http://stackoverflow.com/questions/4931892/why-does-the-division-of-two-integers-return-0-0-in-java) – Mad Physicist Oct 19 '16 at 21:11

1 Answers1

1

The problem is related to data types. Long / float / division, etc.

 System.out.println("Evaluating " + num + " as array of char 1Mil times: " + (float)accumalatedTime/(float)iterations);

Also, a small debugging tip:

print the value of each var (and/or use a debugger) and not just the calculation e.g.

System.out.println("accumulatedTime " + accumalatedTime);
System.out.println("iters " + iterations);
System.out.println("Evaluating " + num + " as array of char 1Mil times: " + (float)accumalatedTime/(float)iterations);
Steve Harrington
  • 942
  • 1
  • 7
  • 18
  • I was using a debugger. Thanks for pointing out the datatype... all it takes is a bit of inexperience to help make problems worse than they really are. – EricO Oct 19 '16 at 21:12