Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Constructor and static method

Tags:

java

When should I use a constructor and when should I use static method?

Can you explain above with small snippet? I skimmed through a few threads but I'm still not clear with this.

like image 400
ram Avatar asked Dec 10 '12 14:12

ram


People also ask

Can static method have constructor in Java?

Java constructor can not be static A constructor is called when an object of a class is created, so no use of the static constructor. Another thing is that if we will declare static constructor then we can not access/call the constructor from a subclass.

What is difference between static method and constructor?

The static factory method has names that clarify the code, unlike the constructors. In the static factory method, we do not need to create a new object upon each invocation i.e object can be cached and reused if required. We can also return the subtype of their return type.

Can we use static method in constructor?

A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically. The user has no control on when the static constructor is executed in the program. A static constructor is called automatically.

Is constructor static or non static in Java?

Not completely sure what you're asking, but constructors aren't static in java (though there is a static block). Constructors setup the object for initial use and are called when instantiated.


2 Answers

Joshua Bloch advises to favor static factory methods instead of constructors (which I think is a good practice). Couple of advantages and disadvantages :

Advantages of static factory methods :

  • unlike constructors, they have names
  • unlike constructors, they are not required to create a new object each time they're invoked (you can cache instances : e.g. Boolean.valueOf(..)
  • unlike constructors, they can return an object of any subtype of their return type (great flexibility)

Disadvantages of static factory methods :

  • They are not really distiguishable from other static methods (it's hard to find out how to initialize an object if you are not familiar with the API)
  • The main disadvantage (if you use only static factory methods, and make constructors private) is that you cannot subclass that class.
like image 128
Ioan Avatar answered Oct 15 '22 17:10

Ioan


Use a public constructor when you only ever want to return a new object that type and you want simplicity.

A good example is StringBuilder as it's mutable and you are likely to want a new object each time.

public String toString() {
    StringBuilder sb = new StringBuilder();
    // append fields to the sb
    return sb.toString();
}

Use a static factor method when you might want to re-use objects (esp if immutable), you might want to return a sub-class or you want descriptice construction. A good example is EnumSet which has a number of static factories which do different things even though some have the same arguments.

EnumSet.noneOf(RetentionPolicy.class);
// has the same arguments, but is not the same as
EnumSet.allOf(RetentionPolicy.class);

In this case, using a static factory makes it clear what the difference between these two ways of construction the set.

Also EnumSet can return two different implementations, one optimised for enums with a small number of values (<= 64) RegularEnumSet and another for many values called JumboEnumSet

like image 38
Peter Lawrey Avatar answered Oct 15 '22 17:10

Peter Lawrey