Java Constructors

.
12 Replies
JavaBot
JavaBot2mo 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
lipOP2mo 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
dan1st2mo 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
lipOP2mo ago
if(this instanceof Yonetici);
else FileStore.addToTheFileConfig(this);
if(this instanceof Yonetici);
else FileStore.addToTheFileConfig(this);
i did this there
dan1st
dan1st2mo ago
oh I see Do you see the ; at the end of the if?
lip
lipOP2mo ago
now i removed the comments and it works but idk if i made a problem
lip
lipOP2mo ago
No description
lip
lipOP2mo ago
yes
dan1st
dan1st2mo 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
lipOP2mo ago
so you mean 'else' will be executed no matter what?
dan1st
dan1st2mo 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
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?