JavaRush /Java Blog /Random EN /Initializing fields

Initializing fields

Published in the Random EN group
Greetings, colleagues. I 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 demo examples, let's create a Java Demo class with a main() method . In the main() method , we'll print something to the screen to make sure the program runs:
public class Demo {
    public static void main(String[] args) {
        System.out.println("Hello JavaRush.");
    }
}
Next, let's create a Java cat class and add several fields to it: name, age.
class Cat {
    String name;
    int age;
}
In the method main()we will create two cats. 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 did not initialize these fields, they received default values. Stringrefers to reference types, and for all reference types the default value is null. intbelongs to primitive types. Each primitive type has its own default value:
Field initialization - 2
How can we pass real values ​​into our fields (for example, the name "Tom" and the age "two years old")? The first way is to initialize the fields immediately during 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
Moreover, 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 method of initialization is through the constructor. Let's create a constructor for the cat with two parameters. Usually the parameter names are made the same as the field names. In this case, you need to use the word to refer to the fields this, and constructor parameters can be accessed in the usual way:
public Cat(String name, int age) {
    this.name = name;
    this.age = age;
}
Again:
Initializing fields - 3
Let's also add an empty constructor without parameters to make our previous example work. In the method main()we will add two more cats using a 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 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