I’m going through the book Just Java 2 but am evidently missing something basic. These are two separate projects. I’ve generated a JAR for the second and added it to the first’s build path. The correct areas are printed but the compiler generates these warnings. How would these be resolved?
// -----------------------------------------------------------
// Testing.java
// -----------------------------------------------------------
public class Testing {
public static void main(String[] args) {
RectangleDFC r = new RectangleDFC(3, 4);
System.out.println(r.Area());
// WARNING: The static method Area() from the type RectangleDFC
// should be accessed in a static way
r.SetSides (10, 10);
// WARNING: The static method SetSides(int, int) from the type
// RectangleDFC should be accessed in a static way
System.out.println(r.Area());
// WARNING: The static method Area() from the type RectangleDFC
// should be accessed in a static way
}
}
// -----------------------------------------------------------
// RectangleDFC.java
// -----------------------------------------------------------
public class RectangleDFC {
int side1;
int side2;
RectangleDFC(int s1, int s2) {
SetSides(s1, s2);
}
public void SetSides(int s1, int s2) {
side1 = s1;
side2 = s2;
}
public int Area() {
return side1 * side2;
}
}
First off; methods in Java should be
lowerCamelCase(), notUpperCamelCase(). Class names should beUpperCamelCase().Second;
should be
and preferably ( if you aren’t modifying them )
and you should just set the
side1andside2inside the constructor instead of the setter.That said, I don’t think you are executing the code you posted, there is no reason those errors should be emitted with the code you posted, you are probably linking to a .jar file with some old code where the
area()method is declaredstatic.Also that book is pretty old in Internet time, there are much better beginner books that cover “modern” Java much better. For example, if the book you are using is using
Enumeration,VectororHashtableput it in the trash and get a newer book.