Defining the constructor of an Object

Asked

Viewed 54 times

2

Hello, I’m doing exercises on constructors in Java but I’m having a question.

I have two classes

Main java.

public class Main {

    public static void main(String[] args) {

        Duck[] d = new Duck[5];
        d[0] = new Duck();
        d[1] = new Duck();
        d[2] = new Duck();
        d[3] = new Duck();
        d[4] = new Duck();
    }
}

Duck.java

public class Duck {

    private int amountOfDucks;

    public Duck() {

        amountOfDucks++;

        if(amountOfDucks >= 1 && amountOfDucks <= 3) {

            System.out.println("Ha poucos patos na lagoa. Quack!");
        }
        else if (amountOfDucks >= 4 && amountOfDucks <= 6) {

            System.out.println("Ha alguns patos na lagoa. " +
                "Quack! Quack!");
        }
        else {

            System.out.println("Ha muitos patos na lagoa!! " +
                "Quack! Quack! Quack! Quack...");
        }

        System.out.println("Quantidade de Patos: "
            + amountOfDucks + "\n");
    }
}

I have the way out

There are few ducks in the pond. Quack!
Quantity of Ducks: 1

There are few ducks in the pond. Quack!
Quantity of Ducks: 1

There are few ducks in the pond. Quack!
Quantity of Ducks: 1

There are few ducks in the pond. Quack!
Quantity of Ducks: 1

There are few ducks in the pond. Quack!
Quantity of Ducks: 1

How do I get an out with the number of ducks amountOfDucks increasing and not just 1?

1 answer

3


Basically the problem is that its variable amountOfDucks is encoded so that each object has its own copy of that variable. Each instance has a different version and its own values. So every time you create a new object Duck it will have the initial value that is 1.

For this value to be shared by all instances you have to declare the field as static. That is, you must create the class as:

public class Duck {

    private static int amountOfDucks = 0;

    public Duck() {

        amountOfDucks++;

        if(amountOfDucks >= 1 && amountOfDucks <= 3) {

            System.out.println("Ha poucos patos na lagoa. Quack!");
        }
        else if (amountOfDucks >= 4 && amountOfDucks <= 6) {

            System.out.println("Ha alguns patos na lagoa. " +
                "Quack! Quack!");
        }
        else {

            System.out.println("Ha muitos patos na lagoa!! " +
                "Quack! Quack! Quack! Quack...");
        }

        System.out.println("Quantidade de Patos: "
            + amountOfDucks + "\n");
    }
}

There yes, when you reference amountOfDucks from any instance you get the same thing, because by declaring as static the field becomes shared, that is, it is something class and not of objects.

One last observation: if the field were public you could reference from other classes. In this case, you reference using the class name before the point, ie, would have to use Duck.AmoutOfDucks.

  • That’s right, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.