patchfox
patchfox

Reputation: 25

Why a constructor with an array of class objects inside has a null value

I learn java for the studies at the moment and I run into a problem. That was foreseeable. I tried several things by my own but I have no more idea to solve the problem now. I think I need help to understand the specific concept of java here.

This is my simple code:

package com.company;

import java.util.Arrays;

public class Verwaltung {

    String vorname, name, geschlecht, geburtsdatum;
    Verwaltung [] verwaltung;



    public Verwaltung(String vorname, String name, String geschlecht, String geburtsdatum) {
        this.vorname = vorname;
        this.name = name;
        this.geschlecht = geschlecht;
        this.geburtsdatum = geburtsdatum;

    }

    public Verwaltung () {
        int i;
        verwaltung = new Verwaltung[] {
                new Verwaltung("Holger", "Fritz", "m", "05.05.1996"),
                new Verwaltung("Holger", "Fritz", "m", "05.05.1996"),
                new Verwaltung("Holger", "Fritz", "m", "05.05.1996"),
                new Verwaltung("Holger", "Fritz", "m", "05.05.1996"),
                new Verwaltung("Holger", "Fritz", "m", "05.05.1996"),
        };
            for (i = 0; i < verwaltung.length;  i++) {
                if (verwaltung[i] != null) {
                    System.out.println(verwaltung[i]);
                }
        }
    }


    @Override
    public String toString() {
        return "Verwaltung{" +
                "vorname='" + vorname + '\'' +
                ", name='" + name + '\'' +
                ", geschlecht='" + geschlecht + '\'' +
                ", geburtsdatum='" + geburtsdatum + '\'' +
                ", verwaltung=" + Arrays.toString(verwaltung) +
                '}';
    }
}

This Class is called in the main Method with the standard constructor

Output

Verwaltung{vorname='Holger', name='Fritz', geschlecht='m', geburtsdatum='05.05.1996', verwaltung=null}
Verwaltung{vorname='Holger', name='Fritz', geschlecht='m', geburtsdatum='05.05.1996', verwaltung=null}
Verwaltung{vorname='Holger', name='Fritz', geschlecht='m', geburtsdatum='05.05.1996', verwaltung=null}
Verwaltung{vorname='Holger', name='Fritz', geschlecht='m', geburtsdatum='05.05.1996', verwaltung=null}
Verwaltung{vorname='Holger', name='Fritz', geschlecht='m', geburtsdatum='05.05.1996', verwaltung=null}

My question is now: why I get the value null of the array "verwaltung" back?

Upvotes: 1

Views: 70

Answers (1)

Eduardo Nobre
Eduardo Nobre

Reputation: 200

Its happen because u print a object of first constructor with args that not initialize the array "verwaltung".

Instance:

 new Verwaltung("Holger", "Fritz", "m", "05.05.1996"),

Print:

System.out.println(verwaltung[i]);

The method toString its called by printLn with instance object of first constructor, wich print ur output.

U are not printing the instance of standard constructor wich initialize the array, but the instance of first constructor.

Upvotes: 2

Related Questions