Java Reference
In-Depth Information
}
long getLong () {
return visibleOnlyInThisClass;
}
}
public class VisibilityExample {
public static void main(String[] args) {
TestClass tc = new TestClass();
tc.setLong(32768);
tc.visibleFromEntirePackage = 3.1415926535;
System.out.println(tc.getLong());
System.out.println(tc.visibleFromEntirePackage);
}
}
Output:
32768
3.1415926535
Fields are typically tied to an object of a class. Each object of a class holds an in-
stance of each field in the class. However, you can define so-called static fields that oc-
cur just once, and with a single value shared by all objects of the given class. Listing
1-4 illustrates the difference.
Listing 1-4 . Static Fields
package org.java8recipes.chapter01.recipe1_04;
class StaticDemo {
public static boolean oneValueForAllObjects = false;
}
public class StaticFieldsExample {
public static void main (String[] args) {
StaticDemo sd1 = new StaticDemo();
 
 
Search WWH ::




Custom Search