getting a value from another class

can you explain me, please, why the console says "null" for the commend: "audi.getTypSamochodu()?

public class Samochod extends Pojazd {
private String typSamochodu;

public Samochod(String kolor, int pojemnosc, int moc){
    super(kolor, pojemnosc, moc);
    String typSamochodu = "nie podano typu samochodu";
}

public String getTypSamochodu(){return typSamochodu;}
public void setTypSamochodu(String typSam){typSam = typSamochodu;}
}



public class Test {
public static void main(String[] args){

    Samochod audi = new Samochod("black", 1600, 102);
    audi.setTypSamochodu("suv");
    System.out.println("color: "+audi.getKolor()+" typ: "+audi.getTypSamochodu());
}
}

1 answer

  • answered 2017-06-17 19:45 GaurZilla

    public void setTypSamochodu(String typSam){typSam = typSamochodu;}
    }
    

    Please update your code to:

    public void setTypSamochodu(String typSam){ typSamochodu = typSam ;}
    }