- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Dynamic Initialization in Java
Dynamic Initialization Creating Denso QR Bar Code In Java Using Barcode drawer for Java Control to generate, create QR image in Java applications. Recognizing Quick Response Code In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. Although the preceding examples have used only constants as initializers, Java allows variables to be initialized dynamically, using any expression valid at the time the variable is declared For example, here is a short program that computes the length of the hypotenuse of a right triangle given the lengths of its two opposing sides: // Demonstrate dynamic initialization class DynInit { public static void main(String args[]) { double a = 30, b = 40; // c is dynamically initialized double c = Mathsqrt(a * a + b * b); Systemoutprintln("Hypotenuse is " + c); Painting Barcode In Java Using Barcode creation for Java Control to generate, create bar code image in Java applications. Bar Code Reader In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. - 44 - QR-Code Generator In Visual C# Using Barcode creation for .NET framework Control to generate, create Denso QR Bar Code image in .NET framework applications. Encode Quick Response Code In Visual Studio .NET Using Barcode encoder for ASP.NET Control to generate, create QR Code image in ASP.NET applications. Here, three local variables a, b,and c are declared The first two, a and b, are initialized by constants However, c is initialized dynamically to the length of the hypotenuse (using the Pythagorean theorem) The program uses another of Java's builtin methods, sqrt( ), which is a member of the Math class, to compute the square root of its argument The key point here is that the initialization expression may use any element valid at the time of the initialization, including calls to methods, other variables, or literals QR-Code Creator In VS .NET Using Barcode maker for .NET Control to generate, create QR Code 2d barcode image in .NET framework applications. QR Code Drawer In VB.NET Using Barcode creator for .NET framework Control to generate, create QR image in .NET framework applications. The Scope and Lifetime of Variables
Draw Code 39 Extended In Java Using Barcode creation for Java Control to generate, create USS Code 39 image in Java applications. UPC A Encoder In Java Using Barcode creation for Java Control to generate, create UPC A image in Java applications. So far, all of the variables used have been declared at the start of the main( ) method However, Java allows variables to be declared within any block As explained in 2, a block is begun with an opening curly brace and ended by a closing curly brace A block defines a scope Thus, each time you start a new block, you are creating a new scope As you probably know from your previous programming experience, a scope determines what objects are visible to other parts of your program It also determines the lifetime of those objects Most other computer languages define two general categories of scopes: global and local However, these traditional scopes do not fit well with Java's strict, object-oriented model While it is possible to create what amounts to being a global scope, it is by far the exception, not the rule In Java, the two major scopes are those defined by a class and those defined by a method Even this distinction is somewhat artificial However, since the class scope has several unique properties and attributes that do not apply to the scope defined by a method, this distinction makes some sense Because of the differences, a discussion of class scope (and variables declared within it) is deferred until 6, when classes are described For now, we will only examine the scopes defined by or within a method The scope defined by a method begins with its opening curly brace However, if that method has parameters, they too are included within the method's scope Although this book will look more closely at parameters in 5, for the sake of this discussion, they work the same as any other method variable As a general rule, variables declared inside a scope are not visible (that is, accessible) to code that is defined outside that scope Thus, when you declare a variable within a scope, you are localizing that variable and protecting it from unauthorized access and/or modification Indeed, the scope rules provide the foundation for encapsulation Scopes can be nested For example, each time you create a block of code, you are creating a new, nested scope When this occurs, the outer scope encloses the inner scope This means that objects declared in the outer scope will be visible to code within the inner scope However, the reverse is not true Objects declared within the inner scope will not be visible outside it To understand the effect of nested scopes, consider the following program: // Demonstrate block scope class Scope { public static void main(String args[]) { int x; // known to all code within main x = 10; if(x == 10) { // start new scope int y = 20; // known only to this block // x and y both known here Making EAN / UCC - 13 In Java Using Barcode generator for Java Control to generate, create GS1 128 image in Java applications. Data Matrix 2d Barcode Generation In Java Using Barcode generator for Java Control to generate, create ECC200 image in Java applications. - 45 - Painting UCC - 14 In Java Using Barcode creation for Java Control to generate, create Case Code image in Java applications. Paint Data Matrix In None Using Barcode drawer for Font Control to generate, create DataMatrix image in Font applications. } // y = 100; // Error! y not known here // x is still known here Systemoutprintln("x is " + x); Bar Code Drawer In .NET Framework Using Barcode generator for .NET Control to generate, create bar code image in .NET applications. Scanning Code39 In C#.NET Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications. Systemoutprintln("x and y: " + x + " " + y); x = y * 2; Code 128 Drawer In Java Using Barcode creator for Android Control to generate, create Code 128 Code Set C image in Android applications. Read Bar Code In VB.NET Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications. As the comments indicate, the variable x is declared at the start of main( )'s scope and is accessible to all subsequent code within main( ) Within the if block, y is declared Since a block defines a scope, y is only visible to other code within its block This is why outside of its block, the line y = 100; is commented out If you remove the leading comment symbol, a compile-time error will occur, because y is not visible outside of its block Within the if block, x can be used because code within a block (that is, a nested scope) has access to variables declared by an enclosing scope Within a block, variables can be declared at any point, but are valid only after they are declared Thus, if you define a variable at the start of a method, it is available to all of the code within that method Conversely, if you declare a variable at the end of a block, it is effectively useless, because no code will have access to it For example, this fragment is invalid because count cannot be used prior to its declaration: // This fragment is wrong! count = 100; // oops! cannot use count before it is declared! int count; Here is another important point to remember: variables are created when their scope is entered, and destroyed when their scope is left This means that a variable will not hold its value once it has gone out of scope Therefore, variables declared within a method will not hold their values between calls to that method Also, a variable declared within a block will lose its value when the block is left Thus, the lifetime of a variable is confined to its scope If a variable declaration includes an initializer, then that variable will be reinitialized each time the block in which it is declared is entered For example, consider this program: // Demonstrate lifetime of a variable class LifeTime { public static void main(String args[]) { int x; for(x = 0; x < 3; x++) { int y = -1; // y is initialized each time block is entered Systemoutprintln("y is: " + y); // this always prints -1 y = 100; Systemoutprintln("y is now: " + y); } Data Matrix ECC200 Scanner In C# Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications. Bar Code Generation In Java Using Barcode creation for BIRT Control to generate, create bar code image in Eclipse BIRT applications. The output generated by this program is shown here: y y y y y y is: -1 is now: 100 is: -1 is now: 100 is: -1 is now: 100 - 46 - As you can see, y is always reinitialized to 1 each time the inner for loop is entered Even though it is subsequently assigned the value 100, this value is lost One last point: Although blocks can be nested, you cannot declare a variable to have the same name as one in an outer scope In this regard, Java differs from C and C++ Here is an example that tries to declare two separate variables with the same name In Java, this is illegal In C/C++, it would be legal and the two bars would be separate // This program will not compile class ScopeErr { public static void main(String args[]) { int bar = 1; { // creates a new scope int bar = 2; // Compile-time error bar already defined! } } }
|
|