Using Initializer Block in class definition - Java Object Oriented Design

Java examples for Object Oriented Design:Class

Introduction

Initializers are executed whenever an instance of a class is created, regardless of which constructor is used to create the instance.

Initializer blocks are similar to variable initializers used to initialize variables.

Demo Code

import java.util.Scanner;

public class PrimeClass {
  private Scanner sc = new Scanner(System.in);

  public int x;

  {/*ww w .ja va2 s .c o m*/
    System.out.print("Enter the starting value for x: ");
    x = sc.nextInt();
  }

}

Related Tutorials