tags:

views:

179

answers:

5

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.

+9  A: 

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.

Thomas Owens
Lol Pwned......
Janie
Uh...what? I know what that means, but I don't get the context.
Thomas Owens
The context would be this isn't really a programming issue, but just a general 'doh'..
meandmycode
Oh. Meh. "Pwned" was a little excessive there, IMO.
Thomas Owens
No it's not. It fits perfectly.
geowa4
+3  A: 

Your class name is InitialValues2 but you're creating an InitialValues object. Replace "InitialValues iv = new InitialValues()" with "InitialValues2 iv = new InitialValues2()"

indyK1ng
+3  A: 

In the main method you are creating a new InitialValues, not an InitialValues2 (the class posted).

William
+4  A: 
class InitialValues { 
    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 
}

You're class is called InitialValues2 You should rename it to InitialValues.

MrHus
Also, if InitialValues is a class that's being used, the object could be changed to "InitialValues2 iv = new InitialValues2();".
mouche
+2  A: 
Legendary Pirate