You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The declarations of instance variables can come at the beginning of the class definition or the end. Cay Horstman, author of <urlhref="https://horstmann.com/corejava/index.html"visual="https://horstmann.com/corejava/index.html">the “Core Java” books</url> puts the declarations at the end of the class. I like them at the very beginning so you see the variables that are declared before you begin looking at the code that uses them. With that in mind the first part of the Fraction class definition is as follows:
Notice that we have declared the numerator and denominator to be <term>private</term>.
151
153
This means that the compiler will generate an error if another method tries to write code like the following:
152
154
</p>
153
155
154
-
155
-
<programxml:id="java-private"language="java">
156
+
<listingxml:id="java-private">
157
+
<programinteractive="activecode"language="java">
156
158
<code>
157
159
Fraction f = new Fraction(1,2);
158
160
Integer y = f.numerator * 10;
159
161
</code>
160
162
</program>
163
+
</listing>
161
164
162
165
<p>
163
166
<idx>getter method</idx>
@@ -167,8 +170,8 @@
167
170
Hence, it is a very common programming practice to both provide <term>getter methods</term> and <term>setter methods</term> when needed for instance variables in Java.
0 commit comments