Sunday, November 19, 2017

Static method Details with Singleton design pattern

What is static method?

In general, main motivation for making a method static is convenience. You can call a static method without creating any object, just by using it's class name.So if you need a method, which you want to call directly by class name, make that method static. Utility classes e.g. java.lang.Math or StringUtils, are good examples of classes, which uses static methods.

What static method does?

  1. Static method doesn't modify state of object. Since state of object is maintained as instance variables
  2. Static method mostly operates on arguments, almost all static method accepts arguments, perform some calculation and return value.

What setConfiguration does?

setConfiguration(Configuration conf) -
Set the static configuration for UGI. In particular, set the security authentication mechanism and the group look up service.
How can we connect to multiple clusters at the same time from single JVM? I think this is a very basic scenario currently not being supported by Hadoop API.
Ans: Hadoop API is using like singleton design pattern here. You cannot do that because if you can imagine ever requiring to use object inheritance or needing to use polymorphism for your method, you should definitely skip the static and make it an instance method.
A good scenerio is described here: Static methods are a code smell

Resource Link:

  1. When to make a method static in Java
  2. Java Singleton Design Pattern Best Practices with Examples
Resource Link: https://stackoverflow.com/a/36910490/2293534

3 comments:

  1. Well explained . Great article on singleton pattern . There is also good singleton pattern example visit Singleton class example   

    ReplyDelete
  2. Well explained . Great article on singleton pattern . There is also good singleton pattern example visit Singleton class example   

    ReplyDelete
  3. Well explained . Great article on singleton pattern . There is also good singleton pattern example visit Singleton class example   

    ReplyDelete