Compiler can't resolve symbol

Hi,

I am working on a exercise from the book Thinking in Java ,and the jdk1.3 compiler can't resolve the varialbe symbols.

I hope this is a simple problem.

Here is the code.

class DataOnly {

public static void main(String[] args) {

DataOnly d = new DataOnly();

int i;

float f;

boolean b;

d.i = 47;

d.f = 1.1f; // the compiler can't resolve any of the symbols i,f,b

d.b = false;

}

}

Here is what the compiler is saying.

DataOnly.java:8: cannot resolve symbol

symbol : variable i

location: class DataOnly

d.i = 47;

^

DataOnly.java:9: cannot resolve symbol

symbol : variable f

location: class DataOnly

d.f = 1.1f;

^

DataOnly.java:10: cannot resolve symbol

symbol : variable b

location: class DataOnly

d.b = false;

^

3 errors

If anyone knows what my problem is please let me know.

Thx

[994 byte] By [cell@techa] at [2007-10-3 2:53:29]
# 1

in this case int i,float f,boolean b are not belongs to the object b.

Because you declare that variables in the main method.

correct way is

class DataOnly

{

int i;

float f;

boolean b ;

public static void main(String[] args)

{

DataOnly d = new DataOnly();

d.i = 47;

d.f = 1.1f;

d.b = false;

}

}

Message was edited by:

JBN

JBNa at 2007-7-14 20:42:30 > top of Java-index,Java Essentials,New To Java...
# 2

A better design.

class DataOnly {

private int i;

private float f;

private boolean b ;

DataOnly(int i, float f, boolean b) {

this.i = i;

this.f = f;

this.b = b;

}

public static void main(String[] args) {

DataOnly d = new DataOnly(47, 1.1f, false);

}

}

An even better design would be to give the variables meaningful names.

floundera at 2007-7-14 20:42:30 > top of Java-index,Java Essentials,New To Java...