This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Because that would defeat the purpose of object-oriented programming.
In your example of DecimalFormat, there's a reason that you need to instantiate the class: the instance holds all the formatting information that you give it. You could use a static class to do the same thing, but then if you wanted to use the class to do different things at different points in the program, you'd need to reconfigure it each time.
The syntax for creating strings is a special case defined by the language. If you couldn't specify string literals by using quote marks, you would have to do something like this:
String str = new String('H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd');
which would be extremely inconvenient.
thisString is a variable which holds a reference to a String object.
The reason is that objects contain data as well as methods. If you can get away without the data, like in Math, then you can have all your methods static. Only most of the time you need the object to encapsulate some data, and the methods use those data, so they can't be static.
Joined: Apr 18, 2011
Thank you very much. That makes a lot of sense, and easy to understand. Thanks! Derek