开发者

Java initializing objects

Ok so say I have a class

class Ghost {   
    private int x, y, di开发者_C百科rection;
    private Color color;

Now say I dont explicitly define a constructor, so it just uses the default one. If I were to create a new Ghost object, what value would direction have? 0 right? But I dont know why I keep getting a value of 1 for direction. Anything off the bat that you think could be wrong?


Can you show us more code? Methinks something else is going on here.

int variables will default initialize to 0 in Java, and if I use your snippet, and instantiate a Ghost, all its int members are 0.

So from your comments, I think there might be an issue with your code, like you are accidentally setting direction when using Ghost.setX or Ghost.setY.

In my extremely simple class implementation, this testing code works exactly as intended.

Edited since code was posted

Okay so I have a class based on your code. I don't see anything wrong with the other functions, since very few classes actually alter direction. I removed those extra functions because they didn't modify direction except for one thing that would set it to 0.

You should try a very reduced test case to see if your code works. Try this:

import junit.framework.Assert;

class Ghost
{
    private int x, y, direction;

    public int getX() { return x; }
    public void setX(int x) { this.x = x; }

    public int getY() { return y; }
    public void setY(int y) { this.y = y; }

    public int getDirection() { return direction; }
    public void setDirection(int direction) { this.direction = direction; }

    public static void main (String [] args)
    {
        Ghost g = new Ghost();
        g.setX(10);
        g.setY(20);

        Assert.assertEquals(g.getDirection(), 0);

        System.out.println(g.getX() + " " + g.getY() + " " + g.getDirection());
    }   
}

Everything still works here, so try that code as is. This is purely your code, and if it works (and it should), then there's other code like a user of your class that is doing something.

Standalone, your code should work just fine.

Use the Debugger

Are you familiar with a debugger? If you are using Eclipse or NetBeans, then you should get friendly with using the debuggers integrated into their environments. This should really help you track down when direction changes values by placing a watch on it.

Using a debugger is way beyond the scope of this answer; it's something you really have to read up on and try yourself.

Here's a link for debugging with Eclipse:

  • http://www.ibm.com/developerworks/library/os-ecbug/


There's something you aren't showing us. Does your getDirection() method simply return direction, or does it do something more exotic? Is there a setter method that does something strange? Does some other part of the class that you did not show us modify direction in some way?

In Java, instance variables of type int are initialized to 0, so you are either not showing us something or you are using some non-standard implementation (almost definitely not the latter).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜