Merl's Blog

Static

Learning Java is interesting. There are a few things that seem natural and a few others that do not.

Natural to so far has been the for loop, while loop, and return for methods have been straightforward. I solved very elementary problems in C, I missed the i++ so it is nice to see it return. The while loops and the return are pretty standard, I believe it is just foreign in Clojure.

The strange thing so far has been compiling, and the fact that you need to announce every part of what you are making next. Take this line for example:

public static int addCoins(int amount, int coin, List<Integer> change){
  //stuff
}

I declare a method named addCoins, normal, with the following characteristics. Writing public allows it to be accessible from anywhere in the code. int returns an integer value. static can be called without creating an object of the class, this is the wildest one. For the parameters, the exact type needs to be specified immediately.

Static

All of that is new but the part that sticks out to me is the static, the ability to call the method without creating an instance is what I hoped I had when I wrote tic tac toe in the style of OOP.

As silly as it is, my biggest mistake writing in OOP was forgetting that I had to write an instance in order for my functions to work. Now with static one can call the method whenever you want. Public alone is like your typical method in python or other languages.

Public methods are like individual houses which are accessible from the street, offering unique functionalities specific to each inhabitant. Public static methods are like public libraries which are central hubs everyone can visit directly, offering shared utilities and resources independent of any particular residence.

Both are quite useful depending on their needs. Of course the houses can be quite necessary when your object needs object specific customization, and the libraries can be great when you need efficiency and shared functionality.

If either one is used incorrectly however coupling can happen in different ways. For the public methods objects can potentially become more dependent on each other’s methods, leading to less modular code. Static methods can still create tight coupling if relied upon heavily by other classes.

It is great to add another tool to the tool shed.

I have begun using it in my Coin Changer Kata.

Best,

Merl