This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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


Win a copy of Java 8 in Action this week in the Java 8 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: 36598
    
  16
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
 
Similar Threads
static Methods vs static variables
isnt it ridiculous?
Need help understanding String instantiation and its static methods
Welcome Kathy Sierra and Bert Bates to our Book Promotion of the Week
Big Problem with decimalFormat