Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scope of the variable

 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

scope of the variable means what?

Please explain.

Thanks
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SearchFirst(←click).

Here's a link to pasting your exact question into Google:
http://www.google.com/search?q=scope+of+the+variable+means+what?
 
James X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi veena bijur,


Variable scope refers to the accessibility of a variable.

Example:
public class MainClass {

public static void main(String[] args) {
for (int x = 0; x < 5; x++) { // beggining of scope for variable x
System.out.println(x);
}//end of scope for variable x
}
//here you cant access x
}


In the above example if you observe variable x can be accessed only with in the for loop because the scope of x is with in the loop.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
scope means availability or visibility or accessibility.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch Veena. Are you absolutely new to the Java language? Where are you learning from? Which book?
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. i got it.

sir,
i am not new to Java ,just from interview point of view learning.

when such question asked was stuck.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't "sir" me Veena. I am yet to be knighted by the Queen of England. Your doubt sounded like a novice's. Hence, I asked. Anyways, let us know in case you are stuck. We are here to help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akhilesh Trivedi wrote:scope means availability or visibility or accessibility.
. . .
No it doesn’t. Visibility and accessibility are different from scope. It might mean availability, but I would go farther and say it means the very existence of the variable. Fields are always in scope for the whole class, so that problem does not exist there. But in a loop, like this one…the value of i is put on the stack, and at the end of the first loop, all records of what is on the stack may be gone. So the value of i can be deleted from the stack, or more precisely replaced by another value. Or, to put it another way, when the scope of i is finished, its very existence is finished too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic