aspose file tools*
The moose likes Associate Certification (SCJA,OCAJ 5/6) and the fly likes Certification Exam SCJA When a variable goes out of scope, it can never go back into scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (SCJA,OCAJ 5/6)
Bookmark "Certification Exam SCJA When a variable goes out of scope, it can never go back into scope" Watch "Certification Exam SCJA When a variable goes out of scope, it can never go back into scope" New topic
Author

Certification Exam SCJA When a variable goes out of scope, it can never go back into scope

Juan C. Gonzalez
Greenhorn

Joined: Oct 04, 2012
Posts: 4

Hello, I'm working for the SCJA exam and I've stumbled into this affirmation in the book SCJA - Sun Certified Java Associate Study Guide - Exam CX-310-019 (McGraw-Hill, 2009) and I have a Oracle Certification Conceptual Question

When a variable goes out of scope, it can never go back into scope.
A. True
B. False
Answer:
True A.Once a variable is out of scope, it can never come back into scope. Its value is lost and the Java Virtual Machine may reallocate the memory the variable occupies.

It looks to me like it seems to contradict what oracle is saying about java scope here "A declaration is said to be in scope at a particular point in a program if and only if the declaration's scope includes that point."

given the example:

Questions:
In the method amIOutOfScope would I have x variable "Out of Scope"?

Is "in scope" not exactly the opposite as "out of scope" (perhaps this is like quantum mechanics can be out and in at the same time...)
Fear:
In the example above you cannot access the x variable in the method amIOutOfScope because it is out of scope.
True
False.

Any help to clarify this would be appreciated.
thanks
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Hi Juan,

Variable x is in scope within main(){} and doesn't go out of scope in that method until the method finishes.

It was never in the scope of amIOutOfScope() in the first place.

Fear:
In the example above you cannot access the x variable in the method amIOutOfScope because it is out of scope.
True
False.


True. Variable x is indeed out of scope of the amIOutOfScope() method, but if you think about it, it has never actually gone out of scope in main() and memory for that variable will be reserved until the program finishes in this case.

Think about declaring a variable in amIOutOfScope(). As soon as you exit the method, poof, it's gone. The JVM can reclaim the memory space it had reserved for it. Does that make sense?

Juan C. Gonzalez
Greenhorn

Joined: Oct 04, 2012
Posts: 4

Hello Paul,

I think the problem is probably my mental concept of "scope" in java (I guess I have a rifle in mind or something), then I get the Oracle definition "A declaration is said to be in scope at a particular point in a program if and only if the declaration's scope includes that point." which pretty much goes with the general assumption of what scope is wikipedia . I can understand the memory allocation for the variable but what I found confusing is the statement found in the book "When a variable goes out of scope, it can never go back into scope", because (I thought) memory allocation for one variable is one thing and the scope is another.

To me, this affirmation is still wrong. I think it is possible that there is an error in the certification question and it is not sufficiently well explained (unlikely) or I'm not grasping the concept properly (very likely)
To me It would make a hell of a lot more sense to state something like "when the last line of a code block is executed all the variables declared inside this block will be out of scope, its values will be lost and the Java Virtual Machine could reallocate the memory the variables occupy"

Anyway thanks a lot for your answer.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Juan C. Gonzalez wrote:It looks to me like it seems to contradict what oracle is saying about java scope...


The two statements which you think are contradictory are in fact talking about scope in two entirely different ways. One is talking about what happens as the code is executed, and says that a variable stops being in scope (for a certain scope) at a certain point in the execution of the code. The other is talking about a static view of the code where a variable is "in scope" for a block because it's declared inside that block.
Juan C. Gonzalez
Greenhorn

Joined: Oct 04, 2012
Posts: 4

Hello Paul

OK that makes more sense.

One is talking about what happens as the code is executed, and says that a variable stops being in scope (for a certain scope) at a certain point in the execution of the code. The other is talking about a static view of the code where a variable is "in scope" for a block because it's declared inside that block.

Do you think the affirmation "When a variable goes out of scope, it can never go back into scope" is right then, or just for the static view of the code part.

I know this is getting too cryptic, thank you!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Certification Exam SCJA When a variable goes out of scope, it can never go back into scope