File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes  scope of the variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " scope of the variable" Watch " scope of the variable" New topic

scope of the variable

veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67

scope of the variable means what?

Please explain.

Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Please SearchFirst(←click).

Here's a link to pasting your exact question into Google:
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi veena bijur,

Variable scope refers to the accessibility of a variable.

public class MainClass {

public static void main(String[] args) {
for (int x = 0; x < 5; x++) { // beggining of scope for variable 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

Joined: Jun 22, 2005
Posts: 1599
scope means availability or visibility or accessibility.

Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Welcome to the ranch Veena. Are you absolutely new to the Java language? Where are you learning from? Which book?

~ Mansukh
veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67
Thanks for the reply. i got it.

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

when such question asked was stuck.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 13, 2005
Posts: 46383
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.
I agree. Here's the link:
subject: scope of the variable
It's not a secret anymore!