concept scope in category java

appears as: scope, scope, The scope, scopes, The scopes, scopes
Java SE 11 Programmer I Certification Guide MEAP V03

This is an excerpt from Manning's book Java SE 11 Programmer I Certification Guide MEAP V03.

An object’s life cycle starts when it’s created and lasts until it goes out of scope or is no longer referenced by a variable. When an object is accessible, it can be referenced by a variable and other classes can use it by calling its methods and accessing its variables. I’ll discuss these stages in detail in the following subsections.

Figure 4.19 The scope of local variable avg is part of the if statement.
Figure 4.20 The scope of the method parameter val, which is defined in the method setTested

The scope of a method parameter may be as long as that of a local variable or longer, but it can never be shorter. The following method, isPrime, defines a method parameter, num, and two local variables, result and ctr:

boolean isPrime(int num) {                  #A
    boolean result = true;                        #B
    if (num <= 1) return false;
    for (int ctr = num-1; ctr > 1; ctr--) {       #C
        if (num%ctr == 0) result = false;
    }
    return result;
}

The scope of the method parameter num is as long as the scope of the local variable result. Because the scope of the local variable ctr is limited to the for block, it’s shorter than the method parameter num. The comparison of the scope of all of these three variables is shown in figure 4.21, where the scope of each variable (defined in an oval) is shown by the rectangle enclosing it.

Figure 4.21 Comparison of the scope of method parameters and local variables
OCA Java SE 8 Programmer I Certification Guide

This is an excerpt from Manning's book OCA Java SE 8 Programmer I Certification Guide.

Figure 3.2. The scope of local variable avg is part of the if statement.
Figure 3.7. The scopes of variables can overlap.

Different local variables can have different scopes. The scope of local variables may be shorter than or as long as the scope of method parameters. The scope of local variables is less than the scope of a method if they’re declared in a sub-block (within braces {}) in a method. This sub-block can be an if statement, a switch construct, a loop, or a try-catch block (discussed in chapter 7).

Play for Java

This is an excerpt from Manning's book Play for Java.

Explaining how to use the debugger is beyond the scope of this book, but this should be enough to get you started debugging with Eclipse or IntelliJ.

We’ll start by explaining controllers, and from there we’ll examine action methods and how we can return results to web clients. We’ll then see how to use routes to link HTTP requests to a controller’s action method. After that, we’ll look at what interceptors are and talk about what scopes are available in Play. All of these concepts are important when processing and responding to client requests.

But as a developer, you don’t really want to manage that dataflow. You want to store data for a certain period of time; for example, for the duration of a request or a browser session. You store that data in a certain scope. Play supports a number of scopes, for which it stores data for a certain lifetime. They’re accessed in a similar way as a Map in Java; you store and retrieve values based on a key.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest