Java Initialization Objects

So let's say I have a class

class Ghost {   
    private int x, y, direction;
    private Color color;

Now say that I do not define the constructor explicitly, so it just uses the default value. If I created a new Ghost object, what does direction matter? 0 right? But I don’t know why I keep getting 1 for the direction. Anything from a bit that you think might be wrong?

0
source share
2 answers

Can you show us more code? Something else is happening here.

intthe variables will be initialized to 0 in Java by default, and if I use your fragment and instantiate Ghost, all its members intare 0.

, , , , , direction Ghost.setX Ghost.setY.

, .

, , . , direction. , , , 0.

, , . :

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());
    }   
}

- , . , ( ), , , - .

, .

? Eclipse NetBeans, , . , direction , .

; , .

Eclipse:

+1

- . getDirection() - ? , - ? - , - ?

Java int 0, -, ( ).

+1

Source: https://habr.com/ru/post/1769899/


All Articles