-1

Currently I have made a class named "FundamentalClass" which contains all the methods and Database related objects. Now I have about 15 different JFrame forms and all these forms are calling and initializing the FundamentalClass when they are opened so that I can access its methods and properties like this

FundamentalClass con = new FundamentalClass();

I just wanted to know that is it the correct way of initializing class in each form when it is opened or there is another better way because in this procedure whenever the form is opened every time it initializes FundamentalClass which have a lot of methods.

I have a MainScreen form which have menus to call these 15 forms. Will it be a better idea to call and initialize FundamentalClass in MainScreen form and pass its object in every JFrame form?

Guys I am not a OOP Expert so please suggest me what would you do if you had 15 forms and all these forms had to call such class? And these forms are opened through the Menus of MainScreen.

Airy
  • 5,484
  • 7
  • 53
  • 78
  • Could you provide more details on the structure? What do you mean by "these forms are calling the `FundamentalClass`"? Does a form use every method in `FundamentalClass`? If not, you could consider splitting your `FundamentalClass` in smaller classes, using `extends` or even an `abstract` class if it fits your purpose. – Danish Ashfaq Sep 11 '14 at 10:17
  • well about 80% methods are somehow being called in every form and already have splitted the class into other smaller class but this FundamentalClass is the one which is 80% important. – Airy Sep 11 '14 at 10:20

1 Answers1

0

Do you need a constructor in the Fundamentalclass? Like this you are creating a new instance of it everytime you write FundamentalClass con = new FundamentalClass();

If you only have methods in there to do something you shouldn't really need to call a new instance everytime.

FundamentalClass con; should do just fine then.

cozmic
  • 178
  • 2
  • 7
  • Although I don't need a constructor but what difference it would make call a new instance without constructor? – Airy Sep 11 '14 at 10:30
  • I meant that you don't need to call the new instance then. At the moment when you are calling methods from FundamentalClass you are calling them from the instance you created in the form you are in. It would be more resource friendly to call methods just from a reference to the class if you can do so. – cozmic Sep 11 '14 at 10:33