File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about Static and nonstatic class methods" Watch "Question about Static and nonstatic class methods" New topic
Author

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.###");

fmt.format(circumference);

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:

Code:
---------
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
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Static and nonstatic class methods