Documentation Archive Developer
Search
PATH  Documentation > WebObjects 4.5 > WebObjects Developer's Guide


Table of Contents Previous Section

Variables and Scope

Each kind of variable has a different scope and a different lifetime. Local variables are only visible inside the block of text in which they are declared. In the example above, localVariable1 is declared at the top of a method. It is accessible within the entire body of that method, including the while loop. It is created upon entry into the method and released upon exit. localVariable2, on the other hand, is declared in the while loop construct. You can only access it within the curly braces for the while loop, not within the rest of the method.

The scope of an instance variable is object-wide. That means that any method in the object can access any instance variable. You can't directly access an instance variable outside of the object that owns it; you must use an accessor method instead. See "Accessor Methods".

The lifetime of an instance variable is the same as the lifetime of the object. When the object is created, all of its instance variables are created as well and their values persist throughout the life of the object. Instance variables are not freed until the object is freed.

As you learned in the chapter "Common Methods":

Thus, the variables you declare at the top of the application script (Application.wos) exist as long as the application is running. The variables you declare at the top of the session script (Session.wos) exist for the length of one session. As new users access your application, new sessions are created, so new copies of the session's instance variables are created too. These copies of instance variables are private to each session; one session does not know about the instance variables of another session. As sessions expire, their instance variables are freed. The variables you declare at the top of a component script are created and released as that component is created and released. Finally, the variables you declare at the top of a direct action script (DirectAction.wos) are created at the beginning of the direct action request-response loop cycle and released at the end of the cycle.

Note: Just how often a particular component object is created depends on whether the application object is caching pages. For more information, see "WebObjects Viewed Through Its Classes".

Table of Contents Next Section