Java Reference
In-Depth Information
Listing 6-3. Default Initialization of Class Fields
// DefaultInit.java
package com.jdojo.cls;
class DefaultInit {
byte b;
short s;
int i;
long l;
float f;
double d;
boolean bool;
String str;
public static void main(String[] args) {
// Create an object of DefaultInit class
DefaultInit obj = new DefaultInit();
// Print the default values for all instance variables
System.out.println("byte is initialized to " + obj.l);
System.out.println("short is initialized to " + obj.s);
System.out.println("int is initialized to " + obj.i);
System.out.println("long is initialized to " + obj.l);
System.out.println("float is initialized to " + obj.f);
System.out.println("double is initialized to " + obj.d);
System.out.println("boolean is initialized to " + obj.bool);
System.out.println("String is initialized to " + obj.str);
}
}
byte is initialized to 0
short is initialized to 0
int is initialized to 0
long is initialized to 0
float is initialized to 0.0
double is initialized to 0.0
boolean is initialized to false
String is initialized to null
Access Level Modifiers for a Class
In Listing 6-1, you created the Human class in the com.jdojo.cls package. You used the Human class in Listing 6-2 to
create its object in the FieldAccessTest class, which is in the same package as the Human class. You had no problem in
compiling and running the following statement in Listing 6-2:
Human jack = new Human();
 
Search WWH ::




Custom Search