View RSS Feed

Java Object

Initializing Fields

Rate this Entry
by , 04-26-2012 at 05:52 PM (519 Views)
For a field, initial value could be provided often in its declaration:
Java Code:
public class BedAndBreakfast {

    // initialize to 10
    public static int capacity = 10;

    // initialize to false
    private boolean full = false;
It would be working well when value of the initialization is present & can be placed at 1 line. But certain limitations are associated with such initialization due to its simplicity. If some logic is required by the initialization then simple assignment will not be adequate. Initialization of the instance variables in constructors could be done where logic or other error handling can be used. For class variables, similar capability could be provided by including static initialization blocks in Java programming language.

Submit "Initializing Fields" to Facebook Submit "Initializing Fields" to Digg Submit "Initializing Fields" to Submit "Initializing Fields" to StumbleUpon Submit "Initializing Fields" to Google