Defining the constructor of an Object

2

Hello, I'm doing exercises on Java builders but I have 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 output

  

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

     

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

     

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

     

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

     

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

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

    
asked by anonymous 27.07.2014 / 01:28

1 answer

3

Basically the problem is that your amountOfDucks variable is encoded so that each object has its 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 should 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");
    }
}

So, when you refer to amountOfDucks from any instance you get the same thing, because when declaring as static the field is shared, that is, it is something of class and not of objects .

One final note: if the field was public you could reference it from other classes. In this case, you reference using the name of the class before the point, that is, you would have to use Duck.AmoutOfDucks .

    
27.07.2014 / 01:54