0

so let's say i have a class called city. what's the difference from where i initialize its fields? e.g.

public class City {
    private String cityName;
    private int population;
    private boolean goodPeopleLiveThere;

    City() {
        cityName = "las vegas";
        population = 603488;
        goodPeopleLiveThere = true;
    }
}

why would i initialize in the constructor rather than the fields or vice versa?

see the ambiguity i face is typically i would set them as parameters in the constructor and then initialize them in the main() when i instantiate my class, but then some tutorials i've seen used initialized them like aforementioned, and i'm yet to fully understand the implications of initialize in fields/constructor rather than in the object.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Bryan Mills
  • 115
  • 9
  • i've read that post before though i didn't necessarily follow the logic since i'm a beginner, so i decided to make one w/ my own code – Bryan Mills Mar 14 '15 at 04:16
  • The short version is that there is no real difference. The longer version is that it can sort of make a difference if you're doing inheritance. – childofsoong Mar 14 '15 at 04:20

4 Answers4

0

Usually, people use that format for functionality. Take the following for example

private int houseNumber;
private String houseStreet;

public House(int houseNumber, String houseStreet) {
    this.houseNumber = houseNumber;
    this.houseStreet = houseStreet;
}

Now this way, you can do things like the following much more easily.

public static void main(String[] args) {
    House randomHouse = new House(12, "Main Street");
    House otherHouse = new House(69, "Random Ave.");
}

instead of having to create a new class for each house.

Constant
  • 780
  • 1
  • 5
  • 19
0

There's no difference with these set variables. There may be a difference, however, if these variables depend on user input. For instance:

public class City{
    private String cityName;
    private int population;
    private boolean goodPeopleLiveThere;
    City(String city, int pop, boolean good)){
        cityName = city;
        population = pop;
        goodPeopleLiveThere = good;
    }
}

Now, in this example, a these variables rely on a value submitted upon instanciation, which is not at all rare when it comes to a constructor.

msant080
  • 19
  • 4
0

There is only a slight difference, besides personal preference. However, fields are initialized before constructor bodies are ran. This can cause errors if one it to override a field in the constructor that was initialized previously.

Most important is that one should be consistent throughout their code.

schmidt73
  • 181
  • 11
0

Initializing object-fields outside of the constructor is acceptable in some cases, for example, the builder-pattern is one of the static factory patterns that uses a nested class to initialize the instance members.

There's no "one better than the other" - simply different methods with pros and cons for each one of them.

When you initialize variables outside the constructor you should be careful not to publish the object before it's fully constructed otherwise you'll get yourself in a mess that will be difficult to debug.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129