OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808) by Bert Bates & Kathy Sierra

OCA Java SE 8 Programmer I Exam Guide (Exams 1Z0-808) by Bert Bates & Kathy Sierra

Author:Bert Bates & Kathy Sierra [Bert Bates]
Language: eng
Format: epub
Tags: Certification, Java
Publisher: McGraw-Hill
Published: 2017-05-11T16:00:00+00:00


As with variables in all Java programs, the variables in this program (s, x, x2, x3, y, and z) all have a scope:

s is a static variable.

x is an instance variable.

y is a local variable (sometimes called a “method local” variable).

z is a block variable.

x2 is an init block variable, a flavor of local variable.

x3 is a constructor variable, a flavor of local variable.

For the purposes of discussing the scope of variables, we can say that there are four basic scopes:

1. Static variables have the longest scope; they are created when the class is loaded, and they survive as long as the class stays loaded in the Java Virtual Machine (JVM).

2. Instance variables are the next most long-lived; they are created when a new instance is created, and they live until the instance is removed.

3. Local variables are next; they live as long as their method remains on the stack. As we’ll soon see, however, local variables can be alive and still be “out of scope.”

4. Block variables live only as long as the code block is executing.

Scoping errors come in many sizes and shapes. One common mistake happens when a variable is shadowed and two scopes overlap. We’ll take a detailed look at shadowing in a few pages. The most common reason for scoping errors is an attempt to access a variable that is not in scope. Let’s look at three common examples of this type of error:

Attempting to access an instance variable from a static context (typically from main()):



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.