• 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

when should we use static modifier

 
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,

Appreciate if someone can explain me the scenarios when static modifier should be used with variables and methods.

Why and when should we make a method static and is there any loophole making a variable or a method static.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't static stuff covered in the SCJP?
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abdul Rahman wrote:Hi,

Appreciate if someone can explain me the scenarios when static modifier should be used with variables and methods.

Why and when should we make a method static and is there any loophole making a variable or a method static.



You can consider the classes as providing a few special services that objects can't provide

Consider the Utility methods that return some value calculated only from data passed to the method as parameters. You should make such methods static.


Consider Math.max(double a, double b) which is a static method. It does not operate on a single instance; indeed, it would not be good if you say new Math().max(a,b) or even a.max(b).All the data that max uses comes from its two arguments, and not from any owning object

Another important point to consider when you make method static is that you must make sure there
is no chance you’ll want it to behave polymorphically.

 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks buddies...i get it now...static modifier is used for utility methods. Methods that return the same result everytime and doesn't depend on the state of the object.

David Newton wrote
Isn't static stuff covered in the SCJP?


David, your comments really kicked me to go through the book again...

And Muhammad Ali Khojaye, thanks for your important suggestion

Another important point to consider when you make method static is that you must make sure there
is no chance you’ll want it to behave polymorphically.

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is just one example.

When I am developing a complex application that has a GUI, I will often have some kind of a JTextArea in the GUI, at least temporarily, and I will write diagnostics information to this JTextArea from many different parts of the application.

OK you cannot access a non-static variable from a static method, so...

I make my JTextArea static, which ensures that I can write to the JTextArea from both static and nonstatic methods in other parts of the application.

When I no longer need to do diagnostics, I will remove the static modifer, or maybe just remove the JTextARea and all the statements that write to it.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is one other reason.

If a method is intended to perform an operation on a primitive data type as opposed to an object, then a static method is often the logical choice, because a non-static method needs an object reference.

For example, the class java.lang.Math contains lots of static methods, such as sqrt( double a). Here, a is a primitive of type double.

The decision of what class to put a static method in is often one of convenience, or logic, as opposed to functionality. For example, you could probably have sqrt in another class, but it is in the Math class because it makes sense to have a single class to house all your static mathematical methods.

p.s. I can see now this is similar to what Muhammed talked about in his post above.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic