JavaRush /Java Blog /Random EN /Field initialization

Field initialization

Published in the Random EN group
Hello colleagues. Decided to show several ways to initialize fields in Java. The article may be useful for those who have just started learning programming. Field initialization - 1To run the demos, let's create a Java class Demo with a main() method . In the main() method , let's print something to the screen to make sure the program starts:
public class Demo {
    public static void main(String[] args) {
        System.out.println("Hello CodeGym.");
    }
}
Next, let's create a cat Java class, add several fields to it: name, age.
class Cat {
    String name;
    int age;
}
main()Let's create two cats in the method . Let's try to display their name and age:
public class Demo {
    public static void main(String[] args) {
        Cat cat1 = new Cat();
        Cat cat2 = new Cat();

        System.out.println("Name первого кота: " + cat1.name);
        System.out.println("Возраст первого кота: " + cat1.age);
        System.out.println();
        System.out.println("Name второго кота: " + cat2.name);
        System.out.println("Возраст второго кота: " + cat2.age);
    }
}

class Cat {
    String name;
    int age;
}
Running the program, we get the output:
Name первого кота: null
Возраст первого кота: 0

Name второго кота: null
Возраст второго кота: 0
Since we didn't initialize these fields, they got default values. Stringrefers to reference types, and for all reference types, the default value is null. intrefers to primitive types. Each primitive type has its own default value:
Field initialization - 2
How can we pass real values ​​to our fields (for example, the name "Tom" and the age "two years")? The first way is to initialize the fields at the time of declaration:
public class Demo {
    public static void main(String[] args) {
        Cat cat1 = new Cat();
        Cat cat2 = new Cat();

        System.out.println("Name первого кота: " + cat1.name);
        System.out.println("Возраст первого кота: " + cat1.age);
        System.out.println();
        System.out.println("Name второго кота: " + cat2.name);
        System.out.println("Возраст второго кота: " + cat2.age);
    }
}

class Cat {
    String name = "Том";
    int age = 2;
}
We run the program and see that the fields are initialized with our values:
Name первого кота: Том
Возраст первого кота: 2

Name второго кота: Том
Возраст второго кота: 2
At the same time, no matter how many cats we create, they will all have the same name and age. That is, we simply changed the values ​​null and 0 to “Volume” and 2. The next way to initialize is through the constructor. Let's create a constructor for the cat with two parameters. Usually, parameter names are made the same as field names. In this case, you need to use the word to refer to the fields this, and the constructor parameters can be accessed in the usual way:
public Cat(String name, int age) {
    this.name = name;
    this.age = age;
}
Again:
Field initialization - 3
Let's also add an empty constructor with no parameters to make our previous example work. Let's add two more cats to the method main()using the constructor with parameters.
public class Demo {
    public static void main(String[] args) {
        Cat cat1 = new Cat();
        Cat cat2 = new Cat();

        System.out.println("Name первого кота: " + cat1.name);
        System.out.println("Возраст первого кота: " + cat1.age);
        System.out.println();
        System.out.println("Name второго кота: " + cat2.name);
        System.out.println("Возраст второго кота: " + cat2.age);

        Cat cat3 = new Cat("Рыжик", 5);
        Cat cat4 = new Cat("Barsik", 9);

        System.out.println();
        System.out.println("Name третьего кота: " + cat3.name);
        System.out.println("Возраст третьего кота: " + cat3.age);
        System.out.println();
        System.out.println("Name четвертого кота: " + cat4.name);
        System.out.println("Возраст четвертого кота: " + cat4.age);
    }
}

class Cat {
    String name;
    int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Cat() {
    }
}
Let's run the program:
Name первого кота: null
Возраст первого кота: 0

Name второго кота: null
Возраст второго кота: 0

Name третьего кота: Рыжик
Возраст третьего кота: 5

Name четвертого кота: Барсик
Возраст четвертого кота: 9
As you can see, cats created in this way have a greater individuality. There are other ways to initialize fields in Java, but these are enough to get you started.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION