#Java Constructors

22 messages · Page 1 of 1 (latest)

unreal hedge
#

.

desert brookBOT
#

This post has been reserved for your question.

Hey @unreal hedge! 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.

unreal hedge
#
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

winter trail
#

but practically, constructors shouldn't add the this object to other stuff, especially static variables or things like that

unreal hedge
#

i did this there

winter trail
#

oh I see

#

Do you see the ; at the end of the if?

unreal hedge
#

now i removed the comments and it works but idk if i made a problem

unreal hedge
winter trail
# unreal hedge 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

unreal hedge
winter trail
#

but if you have something you don't want to do for subclasses, you should use a factory method instead of a public constructor