java:defining_classes_in_java
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:defining_classes_in_java [2020/09/12 00:50] – [Accessors] mithat | java:defining_classes_in_java [2020/09/14 01:38] (current) – [Our first Java class] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
We are going to write a simple class for implementing a counter similar to one of these: | We are going to write a simple class for implementing a counter similar to one of these: | ||
Line 28: | Line 28: | ||
Here's a Java definition for a '' | Here's a Java definition for a '' | ||
- | <file java ClickerCounter.java> | + | <code java> |
public class ClickerCounter { | public class ClickerCounter { | ||
| | ||
Line 41: | Line 41: | ||
} | } | ||
} | } | ||
- | </file> | + | </code> |
- | In Java, class definitions need to go in their own files, and the files need to be called the name of the class with the '' | + | I have deliberately not used any comments in the definition so you can more easily see the code. |
- | In the above, '' | + | In the above, '' |
+ | |||
+ | In actuality, this is a pretty crappy class definition for reasons we'll see later. But for now it's enough to get us started. | ||
==== Instantiation and use ==== | ==== Instantiation and use ==== | ||
- | The above is just a class definition. It doesn' | + | The above is just a class definition. It doesn' |
- | The program below shows you how to make, or **instantiate** a '' | + | <code java> |
+ | var myCounter = new ClickerCounter(); | ||
- | <file java ClickerExample.java> | + | myCounter.reset(); |
- | public class ClickerExample { | + | myCounter.click(); |
+ | System.out.println(myCounter.count); | ||
+ | myCounter.click(); | ||
+ | myCounter.click(); | ||
+ | System.out.println(myCounter.count); | ||
+ | myCounter.reset(); | ||
+ | System.out.println(myCounter.count); | ||
+ | </ | ||
+ | ===== Where does the code go? ===== | ||
+ | |||
+ | ==== The easy-for-beginners way ==== | ||
+ | If there' | ||
+ | |||
+ | <file java ClickerCounter.java> | ||
+ | public class ClickerCounter { | ||
+ | |||
+ | // Class definition (instance members) | ||
+ | int count; | ||
+ | | ||
+ | void click(){ | ||
+ | count++; | ||
+ | } | ||
+ | | ||
+ | void reset(){ | ||
+ | count = 0; | ||
+ | } | ||
+ | |||
+ | // Program' | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
var myCounter = new ClickerCounter(); | var myCounter = new ClickerCounter(); | ||
Line 71: | Line 101: | ||
</ | </ | ||
+ | This isn't generally recommended though because very often you will write more than one custom class in your program. If this is the case, the approach above won't work because Java's rules say any file can only contain at most one public class definition.((You //can// have additional non-public class definitions in a file, but that's something we can't discuss yet.)) | ||
- | ===== Access specifiers | + | ==== The better way ==== |
- | The '' | + | |
- | <file java ClickerExample.java> | + | A better way to manage your programs' |
- | public | + | |
- | public static void main(String[] args) { | + | The following is how to break up the class definition and the "main" stuff. Here is the file that has only the class definition: |
- | var myCounter = new ClickerCounter(); | + | |
- | + | ||
- | myCounter.reset(); | + | |
- | myCounter.count = 492341; | + | |
- | System.out.println(myCounter.count); | + | |
- | myCounter.click(); | + | |
- | myCounter.click(); | + | |
- | System.out.println(myCounter.count); | + | |
- | } | + | |
- | } | + | |
- | </file> | + | |
- | the object will happily oblige the user's wish to set the count to some arbitrary value. That's not something you can typically do with a clicker counter. In other words, the object offers no **protection** of its state. This is easy enough to solve with Java's access modifiers. Let's look at a modified version of our class definition that gives us some protection using access modifiers: | + | <file java ClickerCounter.java> |
+ | public | ||
- | <file java ClickerCounter.java> | + | // Class definition |
- | public class ClickerCounter { | + | int count; |
| | ||
- | | + | void click(){ |
- | + | ||
- | public | + | |
count++; | count++; | ||
} | } | ||
| | ||
- | | + | void reset(){ |
count = 0; | count = 0; | ||
- | } | + | } |
} | } | ||
</ | </ | ||
- | Access modifiers are placed before the start of a member. Java offers four levels of protection, and they just so happen to all start with the letter ' | + | And here is the file that has the " |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | With the changes made above, if we try to run '' | + | <file java ClickerExample.java> |
+ | public class ClickerExample { | ||
- | <code java> | + | public static void main(String[] args) { |
- | myCounter.count | + | |
- | </code> | + | |
- | + | myCounter.reset(); | |
- | it cannot access the '' | + | |
- | + | System.out.println(myCounter.count); | |
- | <WRAP center round important 80%> | + | myCounter.click(); // count is 2 |
- | In Java programming, | + | |
- | + | | |
- | Methods can be at whatever access level is appropriate for their use. If it is part of the class' interface, it should be '' | + | |
- | </WRAP> | + | |
- | + | ||
- | + | ||
- | ===== Accessors and mutators ===== | + | |
- | + | ||
- | ==== Accessors ==== | + | |
- | + | ||
- | If we comment out the offending line above, we will see that the program | + | |
- | + | ||
- | <code java> | + | |
- | System.out.println(myCounter.count); | + | |
- | </ | + | |
- | + | ||
- | The compiler is trying to access '' | + | |
- | + | ||
- | So, once we make a member variable private, how do we get its value?? The answer is we can write a public method that simply returns the value of the member variable: | + | |
- | + | ||
- | <file java ClickerCounter.java> | + | |
- | public class ClickerCounter { | + | |
- | + | ||
- | private int count; | + | |
- | + | ||
- | | + | |
- | public int getCount(){ | + | |
- | | + | |
- | } | + | |
- | + | ||
- | public void click(){ | + | |
- | count++; | + | |
- | } | + | |
- | + | ||
- | public void reset(){ | + | |
- | count = 0; | + | |
} | } | ||
} | } | ||
</ | </ | ||
- | This is an example of an **accessor**: | + | This is the best way to manage |
- | + | ||
- | We can now change our '' | + | |
- | + | ||
- | <code java> | + | |
- | public static void main(String[] args) { | + | |
- | var myCounter = new ClickerCounter(); | + | |
- | + | ||
- | myCounter.reset(); | + | |
- | System.out.println(myCounter.getCount()); | + | |
- | myCounter.click(); | + | |
- | myCounter.click(); | + | |
- | System.out.println(myCounter.getCount()); | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | and life if good. Accessors are sometimes called **getters**, | + | |
- | ==== Mutators ==== | + | |
- | + | ||
- | Let's now add a feature to our counter that lets the user set the maximum count: after the counter reaches the maximum, it should reset. Just like a physical clicker counter that only counts up to, say, 10,000. | + | |
- | + | ||
- | We'll need another member variable to store this upper bound, and we'll have to modify the '' | + | |
- | + | ||
- | <code java> | + | |
- | + | ||
- | private int maxCount; // new member variable | + | |
- | + | ||
- | ... | + | |
- | + | ||
- | // modified click() method | + | |
- | public void click(){ | + | |
- | if (count < maxCount) { | + | |
- | count++; | + | |
- | } else { | + | |
- | count = 0; | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Following best-practices, | + | |
- | + | ||
- | <code java> | + | |
- | /** */ | + | |
- | public setMaxCount(int newMax) { | + | |
- | if (newMax > 0) { | + | |
- | maxCount = newMax; | + | |
- | } else { | + | |
- | System.out.println(" | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | The way the mutator checks the new values is another example of what's needed to make an object *self-governing**. | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | ===== Constructors ===== | + | |
- | + | ||
- | ==== Default constructors ==== | + | |
- | Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. However, | + | |
- | + | ||
- | Adding a constructor to our class definition that does the nefarious deed mentioned above looks like: | + | |
- | + | ||
- | ... | + | |
- | + | ||
- | Note the syntax: A constructor does not have a return type, is declared with '' | + | |
- | + | ||
- | So, in short, constructors are used to _initialize_ objects. They can be used for other purposes as well, but object initialization is the main reason they were invented. | + | |
- | + | ||
- | ==== Parameterized constructors ==== | + | |
- | + | ||
- | ===== Miscellany ===== | + | |
- | + | ||
- | ==== toString() ==== | + | |
- | + | ||
- | One thing you'll want to get into the habit of doing with your class definitions is to write a '' | + | |
- | + | ||
- | <code java> | + | |
- | public String toString() { | + | |
- | return | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Now if we want to output the state of a counter using '' | + | |
- | + | ||
- | <code java> | + | |
- | System.out.println(myCounter); | + | |
- | </ | + | |
- | + | ||
- | Writing a '' | + | |
- | + | ||
- | ----------------------------------------------------------------------- | + | |
- | Copyright © 2011-2018 | + | Copyright © 2020 Mithat Konar. All rights reserved. |
java/defining_classes_in_java.1599871843.txt.gz · Last modified: 2020/09/12 00:50 by mithat