#Java Constructors
22 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @unreal hedge! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
public Musteri(String kullaniciAdi, String tamAdSoyad, int sifre) {
this.kullaniciAdi = kullaniciAdi;
this.tamAdSoyad = tamAdSoyad;
this.sifre = sifre;
uuid = new Random().nextInt(10000, 1000000);
str = "Ad soyad: " + this.getTamAdSoyad() + "\n" + "Kullanıcı adı: " + this.getKullaniciAdi() + "\n" + "Müşteri kimlik numarası: " + this.getUuid() + "\n" + "Hesap mevcut mu: " ;
System.out.println("Yönetici nesnesi oluşturulurken str: " + " " + str);
Test.getMusteriler().add(this);
if(this instanceof Yonetici);
else FileStore.addToTheFileConfig(this);
}```
public Yonetici(String kullaniciAdi, String tamAdSoyad, int sifre, Hesap hesap, int yetkiSeviyesi) {
this.yetkiSeviyesi = yetkiSeviyesi;
this.hesap = hesap;
super(kullaniciAdi, tamAdSoyad, sifre);
//uuid = new Random().nextInt(10000, 1000000);
//str = "Ad soyad: " + getTamAdSoyad() + "\n" + "Kullanıcı adı: " + getKullaniciAdi() + "\n" + "Müşteri kimlik numarası: " + getUuid() + "\n" + "Hesap mevcut mu: " ;
Test.getMusteriler().add(this);
Test.getHesaplar().add(hesap);
Test.getMusteriveHesabi().put(this, this.getHesap());
FileStore.addToTheFileConfig(this);
}```
so i have this 2 constructors right here and Yonetici inherits from the Musteri class
and in Yonetici class it enters super(); section
when it gets into it i want to know what happens
i actually dont want FileStore.addToFileConfig(this); to work if its coming from Yonetici class' super(); section
you could add a check in the super constructor
but practically, constructors shouldn't add the this object to other stuff, especially static variables or things like that
else FileStore.addToTheFileConfig(this);```
i did this there
yes
if(something) doSomething();
means "call doSomething if something is true"
However, if you add a semicolon like
if(something); doSomething();
it's the same as
if(something) { }
doSomething();
so it basically runs the doSomething method regardless of the result of the condition
so you mean 'else' will be executed no matter what?
oh I didn't notce the else block
but if you have something you don't want to do for subclasses, you should use a factory method instead of a public constructor