-1

I just started coding. I want to use a switch statement twice for the same variable, and I was told that to do this the variable would have to be 'in scope'.

Being a beginner, I have no idea what that means. So what does being in scope mean? And, if a variable isn't in scope, how do I make it in scope?

Michael
  • 34,340
  • 9
  • 58
  • 100
Dhruv Erry
  • 99
  • 2
  • 7
  • 1
    Please take a look [here](http://www.java-made-easy.com/variable-scope.html) – npinti Jul 04 '16 at 04:53
  • @npinti you beat me to it lol. anyway, to the OP, as it currently stands, your question will be closed off-topic as too broad because it really is. Please refer to [How to ask](http://stackoverflow.com/help/how-to-ask) for what things are OK to be asked here. – Keale Jul 04 '16 at 04:55
  • Long story short: The scope of a variable defines the section of the code in which the variable is visible. In general, variables defined within a block are not accessible outside that block. You can learn what this means by looking at the links posted above, it is not too difficult. – Moonlit Jul 04 '16 at 04:58
  • Possible duplicate of [Java scope and lifetime of variable](http://stackoverflow.com/questions/26954849/java-scope-and-lifetime-of-variable) – Chris Gong Jul 04 '16 at 05:04
  • 1
    Did you even *try* searching for [`java variable scope`](https://www.google.com/search?q=java+variable+scope)? – Andreas Jul 04 '16 at 05:27

2 Answers2

6

A local variable1 is "in scope" if code can access it and out of scope if it can't. In Java, variables are scoped to the block ({}) they're declared in. So:

void foo() {
    int a = 42;

    if (/*some condition*/) {
        String q = "Life, the Universe, and Everything";

        // 1. Both `a` and `q` are in scope here
        System.out.println(a);
        System.out.println(q);
        if (/*another condition*/) {
            // 2. Both `a` and `q` are in scope here, too
            System.out.println(a);
            System.out.println(q);
        }
    }

    // 3. Only `a` is in scope here
    System.out.println(a);
    System.out.println(q); // ERROR, `q` is not in scope
}

Note (1), (2), and (3) above:

  1. The code can access q because q is declared in the same block as the code; tt can access a because it's declared in the containing block.

  2. The code can access q because it's declared in the containing block; it can access a because it's in the next block out.

  3. The code can access a, but not q, because q isn't declared in the block or any of the blocks (or a couple of other things) containing it.

When figuring out what an unqualified identifier (like a or q above, as opposed to the foo in this.foo or the toLowerCase in q.toLowerCase, which are qualified) is, the Java compiler will look in each of these places, one after the other, until it finds a match:

  • For a variable with that name in the innermost block
  • For a variable with that name in the next block out, and so on
  • For a field2 or method (generally: member) with that name in the current class
  • For a class with that name from a package that's been imported
  • For a package with that name

There are a few others for that list (I'm not going to get into static imports with a beginner).

There's a lot more to scope, I suggest working through some tutorials and/or a beginning Java book for more.


1 "local variable" vs. "variable" - The Java Language Specification uses "variable" in a more general way than most people do in common speech. When I say "variable" in this answer, I mean what the JLS calls a "local variable".

2 "field" - The JLS calls fields "variables" in some places (and "fields" in other places), hence (1) above. :-)

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • I don't think scope and accessibility are the same thing at all. The key to "scope" is that the variable can be referenced by an **unqualified** name. Thus, the public static variable `a` in public class `A` can be accessed from code in class `B` (that is in a separate compilation unit) by `A.a`, but `a` is not in scope in class `B` unless it is statically imported into `B`'s compilation unit. – Ted Hopp Jul 04 '16 at 05:44
  • @TedHopp: There's an equivalence for *variables*, which is what the answer above mainly covers. I'll clarify the list at the end, I was indeed talking about unqualified identifiers. – T.J. Crowder Jul 04 '16 at 05:46
  • I don't think you are using the term _variable_ in a way consistent with the JLS. [Section 4.12.3](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.12.3), for instance, identifies eight kinds of variables, including class and instance variables (a.k.a. `static` and non-`static` fields, respectively). There's no equivalence between scope and access when talking about those kinds of variables. But I agree that OP is probably talking about local variables, in which case there is an equivalence. – Ted Hopp Jul 04 '16 at 05:51
  • @TedHopp: I tend to forget that the JLS uses "variable" where most of us use "field," thanks. (I was just reminded of it the other day, too!) I'll update the above to say *local variable*. – T.J. Crowder Jul 04 '16 at 06:04
0

From Section 6.3 of the Java Language Specification:

The scope of a declaration is the region of the program within which the entity declared by the declaration can be referred to using a simple name, provided it is visible.

This concept of scope applies to many kinds of entities in Java: everything from local variables to top-level classes and packages. Even when just talking about variables, there are many cases, from local variables to statically imported fields from another class to the parameter of an exception handler in a catch clause of a try statement. For details, read the JLS or search the web for "Java scope" and read one or more of the many tutorials on the subject that show up.

Ted Hopp
  • 222,293
  • 47
  • 371
  • 489