{"id":3818,"date":"2014-03-30T05:52:36","date_gmt":"2014-03-30T05:52:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-difference-between-the-new-operator-and-class-getinstance-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:52:36","modified_gmt":"2014-03-30T05:52:36","slug":"what-is-the-difference-between-the-new-operator-and-class-getinstance-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/what-is-the-difference-between-the-new-operator-and-class-getinstance-collection-of-common-programming-errors\/","title":{"rendered":"What is the difference between the new operator and Class.getInstance()?-Collection of common programming errors"},"content":{"rendered":"<p>The <code>new<\/code> operator creates a new object of a type that&#8217;s known statically (at compile-time) and can call any constructor on the object you&#8217;re trying to create. It&#8217;s the preferred way of creating an object &#8211; it&#8217;s fast, optimized, and the JVM does lots of aggressive optimizations on it.<\/p>\n<p><code>Class.forName().getInstance()<\/code> is a dynamic construct that looks up a class with a specific name. It&#8217;s slower than using <code>new<\/code> because the type of object can&#8217;t be hardcoded into the bytecode, and because the JVM might have to do permissions checking to ensure that you have the authority to create an object. It&#8217;s also partially unsafe because it always uses a zero-argument constructor, and if the object you&#8217;re trying to create doesn&#8217;t have a nullary constructor it throws an exception.<\/p>\n<p>In short, use <code>new<\/code> if you know at compile-time what the type of the object is that you want to create. Use <code>Class.forName().getInstance()<\/code> if you don&#8217;t know what type of object you&#8217;ll be making.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The new operator creates a new object of a type that&#8217;s known statically (at compile-time) and can call any constructor on the object you&#8217;re trying to create. It&#8217;s the preferred way of creating an object &#8211; it&#8217;s fast, optimized, and the JVM does lots of aggressive optimizations on it. Class.forName().getInstance() is a dynamic construct that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3818","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3818"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3818\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}