Domanda

What is the difference between

setBorder(new TitledBorder(""));

and

setBorder(BorderFactory.createTitledBorder(""));

and which advantages/disadvantages do they have?

È stato utile?

Soluzione

BorderFactory actually might not create new instances each time you call it, but return a reference to an existing one, therefore saving some resources. See also the javadoc.

You can also check out the actual implementation if you really want to know what's going on inside BorderFactory ;-)

Altri suggerimenti

According to Effective Java, item-1: Consider static factory methods instead of constructors. BorderFactory.createTitledBorder("") is static factory method which has following advantages:

  1. One advantage of static factory methods is that, unlike constructors, they have names.
  2. A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.
  3. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
  4. A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.

For details descriptions, go through the Book.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top