Java: why don’t these variables initialize?

I’m working on an example from Bruce Eckel’s book and i was wondering why the initialized values don’t stick when I output them?

class InitialValues2 { 
    boolean t = true;
    char c = 'x';
    byte b = 47;
    short s = 0xff;
    int i =999; 
    long l =1;
    float f = 3.14f;
    double d =3.14159;
    InitialValues reference; 



    void printInitialValues() { 
        System.out.println("data type       Initial values");
        System.out.println("boolean     " + t); 
        System.out.println("char        [" + c + "]"); 
        System.out.println("byte        " + b); 
        System.out.println("short       " + s); 
        System.out.println("int         " + i); 
        System.out.println("long        " + l); 
        System.out.println("float       " + f); 
        System.out.println("double      " + d);
        System.out.println("reference       " + reference);

    } //end printinitialvalues

    public static void main(String args[]) { 
        InitialValues iv = new InitialValues(); 
        iv.printInitialValues();

        //new InitialValues().printInitialValues();

    } //end main 

}

All the variables output 0 and null values.

Answer

I see one problem. The variables are declared in a class called InitialValues2, yet you are calling the printInitialValues() method on an object that is of the type InitialValues. It appears that you are never calling your printInitialValues() method.

Leave a Reply

Your email address will not be published. Required fields are marked *