0

I have a method that fills in a file with default values and I call that method in the constructor. I want that method to run only once ( I mean by only once so when the user press the play button /in eclipse/ then that method will not run in the second press.

I have tried something like this but it doesn't work :

private boolean defaultExecuted;
public synchronized void defaultStatsFile()
{
    if (defaultExecuted)
    {
        return;
    }
    else
    {
        fileReadNWrite.configWriter("panel1", "non-US incidents");
        fileReadNWrite.configWriter("panel2", "Hoaxes");
        fileReadNWrite.configWriter("panel3", "Under NUFORC investigation");
        fileReadNWrite.configWriter("panel4", "Likliest Shape");
        defaultExecuted = true;
    }

}

Is there a smarter way to fill in a file only once ?

Please if something is not clear comment and I will reply. Any help is appreciated.

Komal12
  • 3,340
  • 4
  • 16
  • 25
Samer34
  • 13
  • 6
  • 1
    this is OK, but if you want it for **all** instantiations of this class then make the `boolean` static. Other ways would be to use a singlton or use `spring` – Scary Wombat Mar 29 '17 at 02:48
  • @ScaryWombat Hi, Thanks for the comment. I tried to make it static but it didn't work. I will try to research singlton . I wished if the static solution worked. Any ideas of possibilities why it didn't work ?! :/ – Samer34 Mar 29 '17 at 02:53
  • You mean the eclipse run button? Pressing it twice would run the app twice so of course the method would get called twice. – pvg Mar 29 '17 at 02:58
  • @pvg sorry, I didn't mean it that way. What I meant is when once it run and they close the program. Then run the program again that method would not be called again. So I want that method to run only once and never again. Thanks – Samer34 Mar 29 '17 at 03:00
  • You're going to have to save the fact the method has been run somewhere. 'never again' is not going to happen, after all, the record of the method having run in a previous invocation can be deleted. – pvg Mar 29 '17 at 03:07
  • 1
    You're probably better off explaining the general problem you're trying to solve. 'never again' is really quite vague. never again on that computer? never again till the heat death of the universe? Why? What exactly are you trying to accomplish? – pvg Mar 29 '17 at 03:11
  • @pvg I was trying to fill in a file with data when the program run then after that it would depend on the user. But I solved the issue, I just added the default values to the file manually rather than coding it. Thanks for your help – Samer34 Mar 29 '17 at 03:40

2 Answers2

2

Better you fill data in static block

like : including proper exception handling in Main class , this way no need to dependent on constructor to load default data in file.

static {
            fileReadNWrite.configWriter("panel1", "non-US incidents");
            fileReadNWrite.configWriter("panel2", "Hoaxes");
            fileReadNWrite.configWriter("panel3", "Under NUFORC investigation");
            fileReadNWrite.configWriter("panel4", "Likliest Shape");
            defaultExecuted = true;
}
Hus Mukh
  • 125
  • 12
  • 1
    This causes the code to be executed when the class is loaded. I'm not sure if this is the most elegant solution when calling from GUI code. This mechanism IS a great way to create java singletons, though! – Nik Corthaut Mar 29 '17 at 05:54
  • @NikCorthaut : He mentioned he need to load it one time , for AWT/SWING base applications main method must be there. Also he can do this in some Util class static method and call it in main method before loading the GUI , it depends on which approach will be better. – Hus Mukh Mar 29 '17 at 05:59
  • I find this is very elegant solution and works fine for me. – Arefe Jun 10 '20 at 08:15
-1

Because all code goes in classes and you can have multiple independend class-loaders, you can not do this in java, you need some resource outside of java.

Grim
  • 1,938
  • 10
  • 56
  • 123