{"id":392,"date":"2021-01-04T17:18:41","date_gmt":"2021-01-04T17:18:41","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=392"},"modified":"2021-01-04T17:28:03","modified_gmt":"2021-01-04T17:28:03","slug":"instance-creation-constructors","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/","title":{"rendered":"Instance Creation &#038; Constructors"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a06ed5496633\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a06ed5496633\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/#What_is_a_constructor\" >What is a constructor?<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/#Implicit_and_Explicit_Constructors\" >Implicit and Explicit Constructors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/#The_Default_Constructor\" >The Default Constructor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/#Multiple_Constructors\" >Multiple Constructors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/#this\" >this()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/instance-creation-constructors\/#%22Copy_Constructors%22\" >\"Copy Constructors\"<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>As we said earlier, we can create an instance (object) by using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>new Circle();<\/strong><\/pre>\n\n\n\n<p>which uses:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the&nbsp;<strong>new&nbsp;<\/strong>operator to&nbsp;<em>allocate space for<\/em>&nbsp;the instance<\/li><li>a&nbsp;<em><strong>constructor<\/strong>&nbsp;<\/em>to&nbsp;<em>initialize<\/em>&nbsp;the instance<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_constructor\"><\/span>What is a constructor?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>It is&nbsp;<em>a special method<\/em><\/li><li>Its&nbsp;<em>name is the same as the class name<\/em>&nbsp;(case-sensitive)<\/li><li>It may have zero or more arguments (just like regular methods)<\/li><li>It has no explicit return type<\/li><li>It must not use a return type of&nbsp;<strong>void<\/strong><\/li><li>It may appear that the constructor returns an instance of its class, but in fact, it&#8217;s the&nbsp;<strong>new&nbsp;<\/strong>operator that does that.<\/li><li>It must&nbsp;<em>not<\/em>&nbsp;use a&nbsp;<strong>return&nbsp;<\/strong>statement to return a value<\/li><li>Strangely, a constructor&nbsp;<em>is not a member of its class<\/em><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Implicit_and_Explicit_Constructors\"><\/span>Implicit and Explicit Constructors<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Remember that, in our class Circle, we had:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nimport java.lang.Math;\n\npublic class Circle\n{\n    \/\/ Accessor methods\n    public double getX()      { return m_x; }\n    public double getY()      { return m_y; }\n    public double getRadius() { return m_r; }\n    \/\/ Mutator methods\n    public void setX(double v)      { m_x = v; }\n    public void setY(double v)      { m_y = v; }\n    public void setRadius(double r) { m_r = r; }\n    \/\/ Operations\/Attributes\n    public double getCircumference()\n                { return 2 * Math.PI * m_r; }\n    public double getArea()\n                { return Math.PI * m_r*m_r; }\n\n    double m_x, m_y; \/\/ coordinates of center\n    double m_r; \/\/ radius\n}\n\n<\/pre><\/div>\n\n\n<p><strong>So where are the constructors in this Circle class?<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Answer:<\/strong>&nbsp;There are no&nbsp;<strong><em>explicit constructors, only implicit ones<\/em><\/strong>.&nbsp;<\/p><\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Default_Constructor\"><\/span>The Default Constructor<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>If a class has no explicitly specified constructors, then a&nbsp;<em><strong>default constructor<\/strong><\/em>&nbsp;is automatically generated for that class by the Java compiler<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A&nbsp;<em>default constructor<\/em>&nbsp;is a constructor that takes zero arguments<\/li><li>It is sometimes called a&nbsp;<strong><em>no-argument<\/em>&nbsp;<\/strong>(or&nbsp;<em><strong>no-arg<\/strong><\/em>) constructor)<\/li><li>In the case of the above Circle class, the default constructor does nothing obvious (note that the class fields will be automatically initialized to zero values).<\/li><li>If you wish to write some explicitly specified constructors&nbsp;<em>and<\/em>&nbsp;a no-argument constructor for a class,&nbsp;<em>you must write the no-argument constructor yourself<\/em>.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Multiple_Constructors\"><\/span>Multiple Constructors<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Sometimes, you need to initialize class instances in different ways. So:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A class may have more than one constructor<\/li><li>Because each constructor must have the exact same name as the class name, each separate constructor is therefore an&nbsp;<em><strong>overloaded method<\/strong><\/em>&nbsp;with a&nbsp;<em><strong>different signature<\/strong><\/em>&nbsp;(overloaded just like regular methods)<\/li><\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [4,5,7,8,10,11,13,14]; title: ; quick-code: false; notranslate\" title=\"\">\npublic class Circle\n{\n    \/\/ Constructors\n    public Circle(double x, double y, double r)\n    { m_x = x; m_y = y; m_r = r; }\n\n    public Circle(double r) \n    { m_x = 0.0; m_y = 0.0; m_r = r; }\n\n    public Circle(Circle c)\n    { m_x = c.m_x; m_y = c.m_y; m_r = c.m_r; }\n\n    public Circle()\n    { m_x = m_y = m_r = 0.0; }\n     \n     \/\/ Accessor methods\n    public double getX()      { return m_x; }\n    public double getY()      { return m_y; }\n    public double getRadius() { return m_r; }\n    \/\/ Mutator methods\n    public void setX(double v)      { m_x = v; }\n    public void setY(double v)      { m_y = v; }\n    public void setRadius(double r) { m_r = r; }\n    \/\/ Operations\/Attributes\n    public double getCircumference()\n                { return 2 * Math.PI * m_r; }\n    public double getArea()\n                { return Math.PI * m_r*m_r; }\n\n    double m_x, m_y; \/\/ coordinates of center\n    double m_r; \/\/ radius\n}\n<\/pre><\/div>\n\n\n<div style=\"height:58px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"this\"><\/span><strong>this()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>But there&#8217;s a better way to write this, to avoid code repetition:The&nbsp;<em>first statement<\/em>&nbsp;of a constructor may be a<em>n explicit call to another constructor of the same class<\/em>, using the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>this( \/* arguments *\/ );<\/strong><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [4,5,6,7,8,9]; title: ; quick-code: false; notranslate\" title=\"\">\npublic class Circle\n{\n    \/\/ Constructors\n    public Circle(double x, double y, double r)\n    { m_x = x; m_y = y; m_r = r; }\n    public Circle(double r) \n    { this(0.0, 0.0, r); }\n    public Circle()\n    { this(0.0, 0.0, 0.0); }\n\n     \/\/ Accessor methods\n    public double getX()      { return m_x; }\n    public double getY()      { return m_y; }\n    public double getRadius() { return m_r; }\n    \/\/ Mutator methods\n    public void setX(double v)      { m_x = v; }\n    public void setY(double v)      { m_y = v; }\n    public void setRadius(double r) { m_r = r; }\n    \/\/ Operations\/Attributes\n    public double getCircumference()\n                { return 2 * Math.PI * m_r; }\n    public double getArea()\n                { return Math.PI * m_r*m_r; }\n\n    double m_x, m_y; \/\/ coordinates of center\n    double m_r; \/\/ radius\n}\n<\/pre><\/div>\n\n\n<p>In this way, you can write a constructor whose code is shared among other constructors &#8212; no need to cut and paste code!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">&#8220;Copy Constructors&#8221;<\/h4>\n\n\n\n<p>In C++, there is another kind of constructor that is commonly defined &#8212; a&nbsp;<em>copy constructor<\/em>, which is usually of the form:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Circle(const Circle &amp;c)\t\/\/ This is valid C++, but invalid Java<\/strong><\/pre>\n\n\n\n<p>In C++, a copy constructor is&nbsp;<em>automatically generated<\/em>&nbsp;for you for each class that does not include an explicitly written copy constructor. <br>(C++ also automatically generates an&nbsp;<em>assignment operator<\/em>&nbsp;for each class that does not include an explicitly written assignment operator.)&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The generated copy constructor does a member-wise (shallow) copy.<\/li><li>In some cases, this is incorrect behavior, and will get you into serious trouble.<\/li><li>In other cases, you don&#8217;t want a copy constructor at all, because it doesn&#8217;t make sense for your particular class.<\/li><li>C++ copy constructors can cause a lot of trouble.<\/li><li>If you don&#8217;t want C++ to generate a copy constructor for you, or if you want to write your own copy constructor, you have to explicitly write your own copy constructor, and play games with it..<\/li><\/ul>\n\n\n\n<p>In Java, you can also create a&nbsp;<em>copy constructor<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Circle(Circle c)<\/strong><\/pre>\n\n\n\n<p>but Java&nbsp;<em>will not automatically generate one<\/em>&nbsp;for you.<\/p>\n\n\n\n<p>If you want a copy constructor for a Java class, you must write one yourself.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [10,11]; title: ; quick-code: false; notranslate\" title=\"\">\npublic class Circle\n{\n    \/\/ Constructors\n    public Circle(double x, double y, double r)\n    { m_x = x; m_y = y; m_r = r; }\n    public Circle(double r) \n    { this(0.0, 0.0, r); }\n    public Circle()\n    { this(0.0, 0.0, 0.0); }\n    public Circle(Circle c)\n    { this(c.m_x, c.m_y, c.m_r); }\n\n     \/\/ Accessor methods\n    public double getX()      { return m_x; }\n    public double getY()      { return m_y; }\n    public double getRadius() { return m_r; }\n    \/\/ Mutator methods\n    public void setX(double v)      { m_x = v; }\n    public void setY(double v)      { m_y = v; }\n    public void setRadius(double r) { m_r = r; }\n    \/\/ Operations\/Attributes\n    public double getCircumference()\n                { return 2 * Math.PI * m_r; }\n    public double getArea()\n                { return Math.PI * m_r*m_r; }\n\n    double m_x, m_y; \/\/ coordinates of center\n    double m_r; \/\/ radius\n}\n<\/pre><\/div>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>This is not normally done.<\/p><\/blockquote>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>&nbsp;The proper way to do this in Java is to use&nbsp;<code><strong>clone()<\/strong><\/code>&nbsp;&#8212; more on cloning, later.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>As we said earlier, we can create an instance (object) by using: new Circle(); which uses: the&nbsp;new&nbsp;operator to&nbsp;allocate space for&nbsp;the instancea&nbsp;constructor&nbsp;to&nbsp;initialize&nbsp;the instance What is a constructor? It is&nbsp;a special methodIts&nbsp;name is the same as the class name&nbsp;(case-sensitive)It may have zero or more arguments (just like regular methods)It has no explicit return typeIt must not use [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":57,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-392","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"As we said earlier, we can create an instance (object) by using: new Circle(); which uses: the&nbsp;new&nbsp;operator to&nbsp;allocate space for&nbsp;the instance a&nbsp;constructor&nbsp;to&nbsp;initialize&nbsp;the instance What is a constructor? It is&nbsp;a special method Its&nbsp;name is the same as the class name&nbsp;(case-sensitive) It may have zero or more arguments (just like regular methods) It has no explicit return&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=392"}],"version-history":[{"count":6,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/392\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/392\/revisions\/400"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/57"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}