This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question about Static and nonstatic class methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about Static and nonstatic class methods" Watch "Question about Static and nonstatic class methods" New topic

Question about Static and nonstatic class methods

derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Hi, I am reading a book on java , and it looks like this is true.

Static methods can be called without an object of that class being created.

such as Math.sqrt()

and Non static methods must have a Class object created to call the class methods.

such as DecimalFormat fmt = new DecimalFormat ("0.###");


the "format" DecimalFormat method is not static so we need the fmt object.

My question is. Why not make all of our methods static so we never need to make an object? please. thank you. Derek

also, my book seems to create objects in 2 ways

way 1: String thisString = "this is a string object";

way 2: Random generator = new Random();

isn't String thisString a variable?

I previously posted this HERE
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Nevermind got an answer here is the answer

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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43929
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.
derek smythe
Ranch Hand

Joined: Apr 18, 2011
Posts: 63
Thank you very much. That makes a lot of sense, and easy to understand. Thanks! Derek
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Question about Static and nonstatic class methods
It's not a secret anymore!