Java Constructors

.
11 Replies
JavaBot
JavaBot18h ago
This post has been reserved for your question.
Hey @lip! Please use /close or the Close Post button 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.
lip
lipOP18h ago
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 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);
}
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);
FileStore.addToFileConfig(this);
to work if its coming from Yonetici class' super(); section
dan1st
dan1st18h ago
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
lip
lipOP18h ago
if(this instanceof Yonetici);
else FileStore.addToTheFileConfig(this);
if(this instanceof Yonetici);
else FileStore.addToTheFileConfig(this);
i did this there
dan1st
dan1st18h ago
oh I see Do you see the ; at the end of the if?
lip
lipOP18h ago
now i removed the comments and it works but idk if i made a problem
lip
lipOP18h ago
No description
lip
lipOP18h ago
yes
dan1st
dan1st18h ago
if(something) doSomething();
if(something) doSomething();
means "call doSomething if something is true" However, if you add a semicolon like
if(something); doSomething();
if(something); doSomething();
it's the same as
if(something) { }
doSomething();
if(something) { }
doSomething();
so it basically runs the doSomething method regardless of the result of the condition
lip
lipOP18h ago
so you mean 'else' will be executed no matter what?
dan1st
dan1st18h ago
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

Did you find this page helpful?