What does SCOPE actually mean in java language?
So what does SCOPE actually mean in java language?
is it like who can access information or what exactually is it?
Can someone please give me more information on the concept of SCOPE.
Thanks very much.
EDIT
I came up with this:
The basic meaning of scope is “who can use or access the variables”. Java class variables have six possible levels of visibility to other classes. The six visibility levels are published, public, protected, default, private and local. Each scope has a different visibility and purpose.
Correct me if im wrong thanks.
So from this piece of code:
Code Java:
1 public class Scope {
2 public static void test(int num) {
3 if (num > 10) {
4 int half = num / 2;
5 System.out.println(“Half num = “ + half);
6 } else {
7 System.out.println(num);
8 }
9 }
10 public static void main(String[] args)
11 {
12 int x = 5;
13 test(x);
14 }
15 }
What is the scope of the specified variables? Specifying the line
numbers where they are visible.
Answer: Scope: Public: Visible to all classes and is it line 1 2 and 5?
Quite new to this stuff, still learning. Thanks for your help.
Re: What does SCOPE actually mean in java language?
In a simple sense, and the more typical context I've seen the word used, scope refers the duration and context of which a variable is accessible. Create a variable in a method, and unless returned by the method the variable looses scope when the method exits. Create a variable within a block of code, and its only accessible within that block. Create a variable in a method, and call another method trying to reference said variable without passing it as a parameter it is out of scope.
Code :
public void method1(){
int x = 0;
method();
}
public void method(){
//x is out of scope
for ( int i = 0; i < 10; i++ ){
int val = 20;
//do something with i
}
//right now, i is out of scope
//right now, val is out of scope
}
And according to my vague definition above ('the duration and context of which a variable is accessible' - so vague it's debatable), access modifiers are another form of varying scope. The list you itemize goes over the what seems like the basic definitions of variable scope, but there are a few other more complex scopes that aren't defined in that list (one example - server/client communications), which result in runtime rather than compile time errors (one could probably debate whether they are technically scope problems - in my opinion they are but in a different sort of context).
Re: What does SCOPE actually mean in java language?
Re: What does SCOPE actually mean in java language?
I guess the last thing to add is that scope != lifetime of the variable.
Lifetime deals with when a particular piece of data exists in memory. A simple example would be to have two variables point to the same object, and let one variable goes out of scope. The object will still exist in memory and can be accessed because it's still "alive".
Even when a variable goes out of scope, the data does not cease to exist immediately (ideally it would be, but that would take too much resources). It's alive until it's released from memory (in Java this is done automatically by the Garbage Collector). An analogy is being trapped (but alive) on a desert island vs. being dead.