Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 7
class SomeClass2
{
static int i = 5;
void print ()
{
System.out.println (i);
}
public static void main (String [] args)
{
System.out.println (i);
}
}
SomeClass2 declares a class field i and demonstrates two common ways to access i -- from an instance or class method (both methods are in the same class as the class field).
To access a class field from an instance method in the same class, you only specify the field's name. To access a class field
from another class's instance method, prefix the class field with the name of the class in which the class field is declared.
For example, specify SomeClass2.i to access i from an instance method in another class -- which is in the same package as SomeClass2, because SomeClass2 isn't declared public.
To access a class field from a class method in the same class, you only specify the field's name. To access a class field from another class's class method, follow the same procedure as you did when accessing the class field from an instance method in another class.
Once a class loads, the JVM allocates memory for each class field and establishes a class field's default value. Interpret a class field's default value just as you would interpret an instance field's default value.
Class fields are the closest things to global variables in Java. Check out the example below:
class Global
{
static String name;
}
class UseGlobal
{
public static void main (String [] args)
{
Global.name = "UseGlobal";
System.out.println (Global.name);
}
}
The above example declares a pair of classes in a single source file: Global and UseGlobal. If you compile and then run the application, the JVM loads UseGlobal and then begins executing the main() method's byte codes. After seeing Global.name, the JVM searches for, loads, and then verifies the Global class. Once Global is verified, the JVM allocates memory for name and initializes that memory to null. Behind the scenes, the JVM creates a String object and initializes that object to all characters between the pair of double quote characters -- UseGlobal. That reference assigns to name. Then, the program accesses Global.name and retrieves the String object's reference, which subsequently passes to System.out.println(). Finally, the contents of the String object appear on the standard output device.
Because neither Global nor UseGlobal are explicitly marked public, you can choose any name for the source file. Compiling that source file results in a pair of class files: Global.class and UseGlobal.class. Because UseGlobal contains the main() method, use that class to run the program. Type java UseGlobal to run the program at the command line.
If you type java Global instead, you would receive the following error message:
Exception in thread "main" java.lang.NoSuchMethodError: main
The word main at the end of the error message indicates that java couldn't find a main() method in class Global.
A constant is a read-only variable; once the JVM initializes that variable, the variable's value cannot change.