I am developing an application in Java, which manages a database of bank's loans. I have some expierience in application developing and I have a question that might be silly but it is something I always talk about since I've been learning developing languages and never got a conviencing answer.
In my program I have many classes and methods, which I use as generic tools. For example, I have a class for Excel writing, a class for file reading/writing, a class which manipulates strings in various ways, a class to show Dialogs/Messages in a default format of mine, a class for specific mathematical functions (like Math), etc.
I cannot imagine those classes as real things (like classes/objects are meant to), but they come to my mind as toolboxes. That is why I make them static. In that way I write for example
excelWriter.create("C:\temp.xls");
excelWriter.append("mydata1\tmydata2\nmydata3\tmydata4");
excelWriter.close();
rather than
ExcelWriter myExcelWriter;
myExcelWriter.create("C:\temp.xls");
myExcelWriter.append("mydata1\tmydata2\nmydata3\tmydata4");
myExcelWriter.close();
This suits me better for the reason that I think of a toolbox like something that is always there for me and I don't need to create an object everytime I want to use it. Isn't C++'s Math the same case and static also? I have discussed it with many of my colleagues and coding-friends and most of them say that I have to create an object because that's what object oriented programming is all about.
I understand what they said in another context: In my spare time I've been developing a card game using VB.net. There I had classes for game, player, deck, hand. The objects I made where many out of every class, because I have many games, players, decks, hands. And they can be imagined as real things. It is specifically very different when you are developing a game. The developing of a game is much more object oriented because it contains real things.
I was wondering, am I somehow terribly wrong here? I would like to hear oppinions about what oop is all about in depth. I would also like to hear what static classes are for.
Thanks
No, a class containing only static member functions would not be the right thing in this case, at least in my opinion. The second ExcelWriter class would be better. Anyway, where are you storing the file handle for your Excel output? Not in a static variable, I hope? That is a prime candidate for a data member for this class.
Try to think, in this case, of the object as representing the output stream that you are writing to. In the future, you might want to open two output streams at the same time, which would not be possible with your "toolbox" class.
Sometimes, classes containing only static member functions can be useful as a way of grouping related functions, but this is rare and often means the class can be redesigned around an object containing data. Static member functions can be useful as a way to have different constructors for a class, for example.