Java Reference
In-Depth Information
short shortval;
/* 16 bits, -32,768 to 32,768
*/
int intval;
/* 32 bits, -2147483648 to
2147483647 */
long longval;
/* 64 bits, -(2^64) to 2^64
- 1 */
float
floatval = 10.123456F; /* 32-bit IEEE 754
*/
double doubleval = 10.12345678987654; /* 64-bit
IEEE 754 */
String message = "Darken the corner where you
are!";
message = message.replace("Darken", "Brighten");
}
}
Variables are subject to the concept of visibility . Those created in Listing 1-2 are
visible from the main() method after they have been created, and they are deallocated
when the main() method ends. They have no “life” beyond the main() method, and
are not accessible from outside of main() .
Variables created at the class level are a different story. Such variables can be
termed as fields , as in fields of the class . Use of a field can be restricted to objects of
the class in which it is declared, to the package in which it is declared, or it can be ac-
cessible from any class in any package. Listing 1-3 shows some of how to control vis-
ibility via the private and public keywords.
Listing 1-3 . Visibility and the Concept of Fields
package org.java8recipes.chapter01.recipe1_04;
class TestClass {
private long visibleOnlyInThisClass;
double visibleFromEntirePackage;
void setLong (long val) {
visibleOnlyInThisClass = val;
 
 
Search WWH ::




Custom Search