views:

96

answers:

4

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;
    }
}
A: 

Use the class name instead of the instance name when indicated.

Thorbjørn Ravn Andersen
I would say the same thing, except that the `Area()` method isn't static in his code sample... Maybe the sample is wrong?
Eric Petroelje
you are right, but in his example there are no class names...
Peter Kofler
Area() is not static, your answer is wrong.
fuzzy lollipop
@fuzzy lollipop, does the code listed generate the warning indicated?
Thorbjørn Ravn Andersen
definitely not ;-)
Peter Kofler
No it doesn't, simple eyeball parsing should reveal that.
fuzzy lollipop
@fuzzy, precisely and you even noticed yourself in your answer, so the source included is not what is referenced where the warning is given. The correct action to the warning is as I've stated.
Thorbjørn Ravn Andersen
Not for the code posted, which is all we have to go on, the problem is the .jar isn't running the code posted, this appears to be the code that the poster __wants__ to be executed.
fuzzy lollipop
+5  A: 

First off; methods in Java should be lowerCamelCase(), not UpperCamelCase(). Class names should be UpperCamelCase().

Second;

int side1;
int side2;

should be

private int side1;
private int side2;

and preferably ( if you aren't modifying them )

private final int side1;
private final int side2;

and you should just set the side1 and side2 inside 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 declared static.

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 Vector or Hashtable put it in the trash and get a newer book.

fuzzy lollipop
+1 for trashing the book. My coworkers liked "Effective Java".
Tim Bender
"Effective Java" is not for beginners. Try "Thinking in Java". I am sure there is a question on that...
Peter Kofler
+4  A: 

The shown code will not generate the warning. Maybe you changed the code and forgot to update the JAR in the classpath of Testing class?

And yes, you definitely should adhere to naming conventions as shown by fuzzy lollipop.

Peter Kofler
+1  A: 

Is there some additional code that you aren't showing here that has Area and SetSides defined as static methods? If so, and that code is higher on the classpath than the version you are showing here, that is the problem. As Peter Kofler mentions the code you are displaying will not generate that warning. To get rid of the warnings you would have to replace r.Area() with Rectangle.Area() and r.setSides(10, 10) with Rectangle.SetSides(10, 10).

That being said it makes no sense for those methods to be static. Also, see fuzzy lollipop's comment for proper Java conventions.

laz