java - Does every static factory methods internally uses new keyword to create object? -


i checked following commonly used static factory methods , each 1 of them internally uses new keyword create object

  1. valueof()

     public static string more ...valueof(char c) {      char data[] = {c};      return new string(data, true);  }   public static string more ...valueof(int i) {      return integer.tostring(i);  } 
  2. tostring() returns return new string(buf, true);

    source: java.lang.string.java

part 2 of relevant source: getinstance()

public static calendar more ...getinstance() {     calendar cal = createcalendar(timezone.getdefaultref(),      locale.getdefault(locale.category.format));     cal.sharedzone = true;     return cal; } 

createcalendar method internally creates calendar object using new keyword.

source: java.util.calendar.java

is there static factory method, internally not create object using new keyword?

you have 3 ways :

1) factory types instantiate known @ compile time use new operator.
best way straight , compiler enforces validity of object creation.

2) in particular cases, types instantiate may not known @ compile time.
in case, reflection unavoidable.
relies on class.forname() retrieve class , class.newinstance() method instantiate class.

look example java.net.inetaddress class.
has loadimpl() factory method works in way.
can see class.forname() , class.newinstance() methods may throw "technical" exceptions , these discovered @ runtime.

static inetaddressimpl loadimpl(string implname) {     object impl = null;      /*      * property "impl.prefix" prepended classname      * of implementation object instantiate,      * delegate real work (like native methods).       * property can vary across implementations of java.      * classes.  default empty string "".      */     string prefix = accesscontroller.doprivileged(                   new getpropertyaction("impl.prefix", ""));     try {         impl = class.forname("java.net." + prefix + implname).newinstance();     } catch (classnotfoundexception e) {         system.err.println("class not found: java.net." + prefix +                            implname + ":\ncheck impl.prefix property " +                            "in properties file.");     } catch (instantiationexception e) {         system.err.println("could not instantiate: java.net." + prefix +                            implname + ":\ncheck impl.prefix property " +                            "in properties file.");     } catch (illegalaccessexception e) {         system.err.println("cannot access class: java.net." + prefix +                            implname + ":\ncheck impl.prefix property " +                            "in properties file.");     }      if (impl == null) {         try {             impl = class.forname(implname).newinstance();         } catch (exception e) {             throw new error("system property impl.prefix incorrect");         }     }      return (inetaddressimpl) impl; } 

3) sometimes, created object new or newinstance() may cached avoid creating multiple times same object.
may make sense immutable objects.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

php - Cannot override Laravel Spark authentication with own implementation -

Qt QGraphicsScene is not accessable from QGraphicsView (on Qt 5.6.1) -