Reputation: 13844
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.
Upvotes: 1
Views: 302
Reputation: 116
You are creating a InitialValues object and calling the constructor for it. But the values you want are in the InitialValues2 class. I am guessing there is some copy pasta error going on.
I may suggest changing your main method to:
public static void main(String args[]) {
InitialValues2 iv = new InitialValues2();
iv.printInitialValues();
}
Upvotes: 2
Reputation: 33378
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.
Upvotes: 4
Reputation: 13632
In the main method you are creating a new InitialValues, not an InitialValues2 (the class posted).
Upvotes: 3
Reputation: 3577
Your class name is InitialValues2 but you're creating an InitialValues object. Replace "InitialValues iv = new InitialValues()" with "InitialValues2 iv = new InitialValues2()"
Upvotes: 3
Reputation: 116161
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.
Upvotes: 9