Goatcat
Goatcat

Reputation: 1143

Make GUI static or not

I'm making a GUI class to handle simple tasks like popping up a window, filling it with a couple of panels and other components, nothing complicated.

I wonder what the best practice is:

Note that I'm new to Java. If I have misunderstood something I'd be glad to learn.

Thanks!

Upvotes: 3

Views: 3007

Answers (6)

user3555216
user3555216

Reputation:

It really depends. For a very tiny project, it's okay to make everything in the main class file and make every component static. However, as the project gets bigger, you will definitely want to add other classes.

I am currently working on a project that behaves better when I integrate all GUI parts into the main class and use other classes to handle the relationship between data. No matter what: GUI should be independent from data, and it's better to handle complicated data by using classes.

Upvotes: 0

M Sach
M Sach

Reputation: 34424

i would go with second approach i.e Instantiate an object of my GUI-class because static methods are not good in terms of TDD Approach. When i say not good it means, static methods are tough to mock some time.

Ideally you should go with instance methods whenever method is dealing with state of object . You should select the static ones in case of utility methods when there is no dependency on state of object.

But when you bring TDD in picture and there are there cascaded static methods , sometimes writing junits are tough in some framework like unitils and various others which does not support mocking the static methods, people usually prefer the instance methods even when they have equally good reason for going towards static methods.

Apart from it with class level methods, you loose other abilities of oops model like polymorphism

Upvotes: 1

Oliver Watkins
Oliver Watkins

Reputation: 13509

I always use a thread safe singleton to delegate events from one part of the GUI to another part of the GUI.

Upvotes: 0

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

I would propose to have a single static method like "getInstance" somewhere and use normal OOP elsewhere. Having everything static inside GUI may cause problems later if you discover you need inheritance, polymorphism or some other usual features of OOP.

Upvotes: 1

Evgeniy Fitsner
Evgeniy Fitsner

Reputation: 946

You should instantiate an object of GUI on demand, and use events for exchange of messages or actions. Static or not - it's up to you.

Upvotes: 0

LastFreeNickname
LastFreeNickname

Reputation: 1455

I strongly advise you to do it the "usual" way. While it is generally a good approach to use static methods (!) where possible (externalisation into util classes, better testing possibilities, easier reuse) I would not recommend it for your GUI. If you would make your GUI static, it would act like a singleton, thus taking you the chance to make multiple instances of e.g. your windows, frames etc. at the same time.

Upvotes: 0

Related Questions