• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about Static and nonstatic class methods

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. That makes a lot of sense, and easy to understand. Thanks! Derek
reply
    Bookmark Topic Watch Topic
  • New Topic