What Is 'Scope' in Java

What is 'scope' in Java?

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. :-)

What is the difference between scope and block?

a scope is where you can refer to a variable.
a block defines a block scope a variable defined inside a block will be defined only inside that block and you can't reference it after the end of block.

so in this code if you try something like:

x = 10;
if(x ==10) { // start new scope
int y = 20; // known only to this block
x = y * 2;
}

y = 5; // error y is out of scope, not it is not defined

because what you have here is a local scope

other kinds of scope in java are class scope (for example), a member of a class has a class scope so it is accessible anywhere inside a class.

the basic rules for scope are:

  1. The scope of a parameter declaration is the body of the method in
    which the declaration appears.
  2. The scope of a local-variable declaration is from the point at which
    the declaration appears to the end of that block.
  3. The scope of a local-variable declaration that appears in the
    initialization section of a for statement’s header is the body of
    the for statement and the other expressions in the header.
  4. A method or field’s scope is the entire body of the class. This
    enables non-static methods of a class to use the fields and other
    methods of the class.

What is the default scope of a method in Java?

The default scope is package-private. All classes in the same package can access the method/field/class. Package-private is stricter than protected and public scopes, but more permissive than private scope.

More information:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

http://mindprod.com/jgloss/scope.html

Scope of variable declared inside a for loop

Local variables are declared in methods, constructors, or blocks.

From that it's clear that, All block variables are local variable's.

As per definition of Block

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

So

{   //block started

} //block ended

What ever the variables declared inside the block ,the scope restricted to that block.

for(int i=0; i<10;i++){
int j=0;
}

So J scope is restricted to inside that block. That is for loop.

for(int i=0; i<10;i++){
int j=0;
//do some thing with j ---> compiler says "yes boss"
}
//do some thing with j ---> compiler says "Sorry boss, what is j ??"

Local variable scope in Java vs Python

Python variables are scoped to the innermost function or module; control blocks like if and while blocks don't count.

What's the scope of a Python variable declared in an if statement?

Java scope and lifetime of variable

Well, as you see, that's not how it works: the scope of the variable is the block where it is declared, including any sub block.

Modifying the variable in a sub block modifies it for all of its scope. A copy of the variable is not made everytime a new block starts.

do not understand scope of objects in java (java newbie confusion)

Once Object is out of scope and it will be ready for garbage collection untill GC collect the detached object, will occupy the memory.

What is the scope of a java variable in a block?

java variables do have a block scope but if you notice int a is already defined in scope

  { 
int a = 0;
{
{
}
}




}

all subscopes are in scope of the uppermost curly braces. Hence you get a duplicate variable error.

Scope of private variables within inner classes

The fundamental aspect of this is that inner classes (as opposed to static nested classes) are part of their enclosing class. They aren't separate from it, or from each other. So just like other parts of the enclosing class (constructors and methods) have access to all of its private information, so do all the members of the inner classes. Inner classes are, in some sense, a bit of a fiction that we use as a convenient abstraction mechanism. And since inner classes are part of the enclosing class, their private information is its private information, and so is shared with other inner classes.



Related Topics



Leave a reply



Submit