EyeMaze
EyeMaze

Reputation: 143

Issue with constructor in java

I've started learning java just recently so forgive me if it's stupid problem, I have 3 classes in 3 files. First one

package PLAYLIST1;

public class Utwor {
    private String tytul1;
    private String wykonawca2;
    private Integer rok3;

    Utwor(String x, String y, Integer z){
        x=tytul1;
        y=wykonawca2;
        z=rok3;
    }
    public String tytul(){
        return tytul1;      
    }
    public String wykonawca(){
        return wykonawca2;
    }
    public Integer rok(){
        return rok3;
    }
    public void piosenka(){
         System.out.println(tytul1+" ("+wykonawca2+") ["+rok3+"]");
    }

}

Second

package PLAYLIST1;

import java.util.Arrays;

 class Playlist {
    //ArrayList<String> listaUtworow;
    Utwor[] listaU = new Utwor[3];
    private String lista;
    private int ileJuzJest = 0;



    Playlist(String nazwaListy) {
        //listaUtworow = new ArrayList<String>();
        lista = nazwaListy;
    }

    public String Nazwa() {
        return lista;
    }

    public void dodajUtwor(String tytul, String album, Integer rok) {
        listaU[ileJuzJest] = new Utwor(tytul,album ,rok);
        ileJuzJest++;
        System.out.println("Dodano utwór: '"+tytul+"' do listy: "+lista);
    }



    public void wyswietlListe() {
        if (ileJuzJest==0) {
            System.out.println("Lista jest pusta");
        }
        System.out.println("Utwory z listy "+lista+":");
        for (int i=0; i<ileJuzJest; i++) {
             listaU[i].piosenka();
        }
    }
}

And final with main

package PLAYLIST1;

public class ZarzadzaniePlaylistami {

    public static void main(String[] args) {
        Playlist p1 = new Playlist("Rock");
        Playlist p2 = new Playlist("Pop");

        System.out.println(p1.Nazwa());
        System.out.println(p2.Nazwa());
        p1.dodajUtwor("Stockholm Syndrome","Absolution",2004);
        p1.dodajUtwor("Absolution","Going to Hell",2014);
        p1.dodajUtwor("Loud Like Love","Loud Like Love",2013);
        p2.dodajUtwor("Blank Space","A",2014);
        p2.dodajUtwor("Sugar","B",2013);
        p2.dodajUtwor("Budapest","C",2012);
        p1.wyswietlListe();
        p2.wyswietlListe();




    }

}

As I run program the result is:

Rock
Pop
Dodano utwór: 'Stockholm Syndrome' do listy: Rock
Dodano utwór: 'Absolution' do listy: Rock
Dodano utwór: 'Loud Like Love' do listy: Rock
Dodano utwór: 'Blank Space' do listy: Pop
Dodano utwór: 'Sugar' do listy: Pop
Dodano utwór: 'Budapest' do listy: Pop
Utwory z listy Rock:
null (null) [null]
null (null) [null]
null (null) [null]
Utwory z listy Pop:
null (null) [null]
null (null) [null]
null (null) [null]

Why those variables doesn't save in Utwor class? I've been struggling with this for hour and I'm not going to solve it on my own, please help.

Upvotes: 2

Views: 53

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285440

Backwards assignments in your Utwor constructor as you're assigning the fields (null) the parameter variables:

Utwor(String x, String y, Integer z){
    x=tytul1;
    y=wykonawca2;
    z=rok3;
}

instead, you should assign the parameters to the fields, and so it should be:

Utwor(String x, String y, Integer z){
    tytul1 = x;
    wykonawca2 = y;
    rok3 = z;
}

Interestingly, you get it right in your Playlist class.

Nicely asked question, by the way.

Upvotes: 3

Related Questions