Reputation: 453
I'm sure I've made a simple mistake here but I'm new to Java so looking for some help.
I have one class Cat with:
public class Cat {
public double weight = 10.0;
}
and another class Heavy with:
public class Heavy {
double gain = 10.0;
public void total() {
weight = weight + gain;
}
}
When I try to compile Heavy.java I'm getting an error saying it cannot find the weight symbol (variable). Both files are in the same directory and the cat.java file has been compiled. What am I missing? Appreciate any advice!
Upvotes: 0
Views: 792
Reputation: 770
public class Heavy {
double gain = 10.0;
Cat aCat = new Cat();
public void total() {
aCat.weight = aCat.weight + gain;
}
}
You might want something like this. Instantiate the Cat object in the heavy class to use weight.
Upvotes: 0
Reputation: 143
weight
is a property of Cat
To be able to use it in Heavy class create an instance of cat like this
:
public class Heavy {
double gain = 10.0;
Cat cat = new Cat();
public void total() {
cat.weight = cat.weight + gain;
}
}
Upvotes: 2