{"id":381,"date":"2021-01-04T17:01:50","date_gmt":"2021-01-04T17:01:50","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=381"},"modified":"2021-01-04T17:01:52","modified_gmt":"2021-01-04T17:01:52","slug":"class-members","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/class-members\/","title":{"rendered":"Class Members"},"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-6a06ee0772091\" 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-6a06ee0772091\"  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\/class-members\/#Instance_Members\" >Instance Members<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/class-members\/#Instance_Methods_and_this\" >Instance Methods and this<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/classes-objects\/class-members\/#this\" >this:<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Consider the class\u00a0<strong>Circle<\/strong>:<\/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\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\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    \/\/ Data\n    double m_x, m_y; \/\/ coordinates of center\n    double m_r; \/\/ radius\n}\n<\/pre><\/div>\n\n\n<p>Circle has a number of&nbsp;<em>members<\/em>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>member variables<\/em>,&nbsp;<em>data members<\/em>, or&nbsp;<em>fields<\/em><ul><li><strong>m_x<\/strong>,&nbsp;&nbsp;<strong>m_y<\/strong>, and&nbsp;<strong>m_r<\/strong><\/li><\/ul><\/li><li><em>member functions<\/em>, or&nbsp;<em>methods<\/em><ul><li><em>accessor methods:<\/em><ul><li><strong>double getX()<\/strong><\/li><li><strong>double getY()<\/strong><\/li><li><strong>double getRadius()<\/strong><\/li><\/ul><\/li><li><em>mutator methods:<\/em><ul><li><strong>void setX(double v)<\/strong><\/li><li><strong>void setY(double v)<\/strong><\/li><li><strong>void setRadius(double r)<\/strong><\/li><\/ul><\/li><li>other methods:<ul><li><strong>double getCircumference()<\/strong><\/li><li><strong>double getArea()<\/strong><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Question:<\/u><\/em><\/strong>\u00a0Are\u00a0<strong><code>getCircumference()<\/code><\/strong>\u00a0and\u00a0<strong><code>getArea()<\/code>\u00a0<\/strong>operations or accessor methods??<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Instance_Members\"><\/span>Instance Members<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>What does being an instance member mean?<\/p>\n\n\n\n<p>Here&#8217;s a picture that will help you conceptualize things (I hope!).<\/p>\n\n\n\n<p>We have a class defined as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>class Circle\n{\n  \/\/...\n  double m_r;\n  \/\/...\n}<\/strong><\/pre>\n\n\n\n<p>(we&#8217;re concentrating on the important parts, and ignoring the details)<\/p>\n\n\n\n<p>and we create three instances of this class:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Circle c1 = new Circle();\nCircle c2 = new Circle();\nCircle c3 = new Circle();<\/strong><\/pre>\n\n\n\n<p>Here&#8217;s what the result looks like:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"502\" height=\"382\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/instvar.gif\" alt=\"\" class=\"wp-image-382\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:40px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>That is, each instance retains its own copy of the instance variable <strong>m_r<\/strong>, and thus the value of <strong>m_r<\/strong> is specific to each instance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Instance_Methods_and_this\"><\/span>Instance Methods and this<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let&#8217;s show the\u00a0<code><strong>Circle<\/strong><\/code>\u00a0class again:<\/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<\/pre><\/div>\n\n\n<p>The above methods are actually&nbsp;<em>instance methods.<\/em><\/p>\n\n\n\n<p>This means that each method &#8216;knows&#8217; which instance it is being called against.<\/p>\n\n\n\n<p>For example, if we have code like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Circle c1 = new Circle();\nCircle c2 = new Circle();\ndouble r1 = c1.getRadius();\ndouble r2 = c2.getRadius();<\/strong><\/pre>\n\n\n\n<p>the calls to each&nbsp;<strong>getRadius()<\/strong>&nbsp;method will return the size of the radius&nbsp;<em>for its respective instance<\/em>.<\/p>\n\n\n\n<p>The code for&nbsp;<strong>getRadius()<\/strong>&nbsp;looks like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public class Circle\n{\n \/\/ ...\n public double getRadius() { return m_r; }\n \/\/ ...\n}<\/strong><\/pre>\n\n\n\n<p>which could have been written:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public class Circle\n{\n \/\/ ...\n public double getRadius() { return <em>this<\/em><\/strong><em><strong>.<\/strong><\/em><strong>m_r; }\n \/\/ ...\n}<\/strong><\/pre>\n\n\n\n<p>Essentially, the call to the method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>double r1 = c1.getRadius();<\/strong><\/pre>\n\n\n\n<p><em>appears<\/em>&nbsp;as if it were being called like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>double r1 = getRadius(<\/strong><em><strong>c1<\/strong><\/em><strong>);<\/strong><\/pre>\n\n\n\n<p>and as if the method were defined:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public double getRadius(<\/strong><em><strong>Circle this<\/strong><\/em><strong>) <\/strong>\n<strong>{ return <\/strong><em><strong>this.<\/strong><\/em><strong>m_r; }<\/strong><\/pre>\n\n\n\n<p>In other words, it is as if there is a hidden parameter being implicitly passed to the function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"this\"><\/span><strong>this<\/strong>:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>is an implicitly declared variable<\/li><li>is local to each instance method<\/li><li>references the instance against which the method was invoked<\/li><li>is normally implicit;&nbsp; however, there are times when you need to use&nbsp;<strong>this&nbsp;<\/strong>explicitly (see later).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Initializers and final Values<\/h2>\n\n\n\n<p>You may:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>set the value of an instance variable in a <em><strong>constructor<\/strong><\/em><\/li><\/ul>\n\n\n\n<p>or:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>initialize it in the class&nbsp;<\/li><\/ul>\n\n\n\n<p>The latter is preferred, especially if the instance variable is typically being initialized to the same value every time.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>double m_radius = 0.0;\nint    m_countDownStart = 10;<\/strong><\/pre>\n\n\n\n<p>You may also declare an instance variable as&nbsp;<strong>final<\/strong>. Here&#8217;s an example from the Java&nbsp;<strong>Math<\/strong>&nbsp;class:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public final static double PI = 3.14159265358979323846;<\/strong><\/pre>\n\n\n\n<p><strong>final<\/strong>&nbsp;means that it&#8217;s the &#8216;final value&#8217; &#8212; in other words, it&#8217;s a constant, and once initialized can&#8217;t be changed.<\/p>\n\n\n\n<p><strong>final<\/strong>&nbsp;variables&nbsp;<em>must be initialized<\/em>; if you forget:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>static final double m_interestRate;<\/strong><\/pre>\n\n\n\n<p>you&#8217;ll get an error:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>'final' members must be initialized<\/strong><\/pre>\n\n\n\n<p>Such&nbsp;<strong>final&nbsp;<\/strong>instance variables are initialized when the instance is created.<\/p>\n\n\n\n<p>JDK Version 1.1 introduced the concept of\u00a0<em>blank finals<\/em>, which allow you to not include an initializer on the declaration of a final variable, but that variable must still have a value assigned to it before it is used.\u00a0 I doubt you&#8217;ll find a reason to do this, except in very unusual circumstances.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the class\u00a0Circle: Circle has a number of&nbsp;members: member variables,&nbsp;data members, or&nbsp;fieldsm_x,&nbsp;&nbsp;m_y, and&nbsp;m_rmember functions, or&nbsp;methodsaccessor methods:double getX()double getY()double getRadius()mutator methods:void setX(double v)void setY(double v)void setRadius(double r)other methods:double getCircumference()double getArea() Question:\u00a0Are\u00a0getCircumference()\u00a0and\u00a0getArea()\u00a0operations or accessor methods?? Instance Members What does being an instance member mean? Here&#8217;s a picture that will help you conceptualize things (I hope!). We have [&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-381","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":"Consider the class\u00a0Circle: Circle has a number of&nbsp;members: member variables,&nbsp;data members, or&nbsp;fields m_x,&nbsp;&nbsp;m_y, and&nbsp;m_r member functions, or&nbsp;methods accessor methods: double getX() double getY() double getRadius() mutator methods: void setX(double v) void setY(double v) void setRadius(double r) other methods: double getCircumference() double getArea() Question:\u00a0Are\u00a0getCircumference()\u00a0and\u00a0getArea()\u00a0operations or accessor methods?? Instance Members What does being an instance member mean?&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/381","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=381"}],"version-history":[{"count":1,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/381\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/381\/revisions\/383"}],"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=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}