0

I have a class called NewSale which extends JFrame

The JFrame is used by the user to create a sale (add products, choose payment method, etc).

When the user starts a new sale the software instantiates the NewSale class.

But when a NewSale is already instantiated the software disposes the older one and starts a new one.

Like this:

// declaration
public static NewSale newSale;

// the menu item calls this method
public static void startNewSale(){
    try{
        newSale.dispose();
    }catch(Exception e{
    }

    newSale = newSale();
}

My question is: Is this a good practice or is there a better way to do it?

konqi
  • 5,137
  • 3
  • 34
  • 52
Jao Assy
  • 115
  • 2
  • 6
  • 2
    To a certain degree, we can't tell that. You see; you make up your application; and if you think that dropping an existing "NewSale" when a new one is requested; then you might have reasons to do so?! In other words: I wouldn't do it this way. But in order to say more, a bit more context would be required. Like - why do want/have to disallow more than one NewSale at the time? – GhostCat Oct 18 '15 at 19:22
  • `static` variables & empty catch blocks are in most scenarios not good practice. – zapl Oct 18 '15 at 19:30
  • Personally, I'd be reducing the number of windows you have to show the user. Instead of simply creating a new window, why not just reset the form? It won't producer a flicker as the window is closed and reopened – MadProgrammer Oct 18 '15 at 19:50
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Oct 18 '15 at 20:26
  • @zapl Why static variables aren't a good practice? – Jao Assy Oct 23 '15 at 03:35
  • http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil especially if you have multiple frames, storing their state in a global variable rather than keeping track of individual frames with individual objects can lead to problems and inconsistencies. In your case that's desired so static works. But its still a limit. You can't start your program twice in parallel – zapl Oct 23 '15 at 06:15

0 Answers0