Default Initialization of Class Fields - Java Object Oriented Design

Java examples for Object Oriented Design:Field

Description

Default Initialization of Class Fields

Demo Code

public class Main {  
  byte b;/*  w  w w. j  a  v  a 2 s  .c  om*/
  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
    Main obj = new Main();
    
    // 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);
  }
}

Result


Related Tutorials