java:defining_classes_in_java
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
java:defining_classes_in_java [2020/09/12 02:56] – [Where does the definition go?] mithat | java:defining_classes_in_java [2020/09/12 03:32] – [Instantiation and use] 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 43: | Line 43: | ||
</ | </ | ||
- | This definition states that an instance of a ClickerCounter will consist of a '' | + | I have deliberately not used any comments in the definition so you can more easily see the code. |
- | ==== Where does the definition go? ==== | + | In the above, '' |
- | === The easy-for-beginners way === | + | In actuality, this is a pretty crappy class definition |
- | If there' | + | |
- | <file java ClickerCounter.java> | + | +==== Instantiation and use ====+ |
- | tbd | + | |
- | </ | + | |
- | This isn' | + | The above is just a class definition. It doesn' |
- | === The better way === | + | <code java> |
+ | var myCounter | ||
- | 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 '' | + | myCounter.reset(); |
+ | myCounter.click(); // count is 1 | ||
+ | System.out.println(myCounter.count); | ||
+ | myCounter.click(); // count is 2 | ||
+ | myCounter.click(); | ||
+ | System.out.println(myCounter.count); | ||
+ | myCounter.reset(); | ||
+ | System.out.println(myCounter.count); | ||
+ | </ | ||
- | In the above, '' | + | ===== Where does the code go? ===== |
- | ==== Instantiation and use ==== | + | ==== The easy-for-beginners way ==== |
+ | If there' | ||
- | The above is just a class definition. It doesn' | + | <file java ClickerCounter.java> |
+ | public | ||
- | The program below shows you how to make, or **instantiate** a '' | + | // Class definition (instance members) |
- | + | int count; | |
- | <file java ClickerExample.java> | + | |
- | public class ClickerExample | + | 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 86: | 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 | + | var myCounter = new ClickerCounter(); |
- | </code> | + | |
- | + | | |
- | it cannot access the '' | + | myCounter.click(); // count is 1 |
- | + | | |
- | <WRAP center round tip 80%> | + | myCounter.click(); |
- | In Java programming, | + | |
+ | | ||
+ | | ||
+ | System.out.println(myCounter.count); | ||
+ | | ||
+ | } | ||
+ | </file> | ||
- | Methods can be at whatever access level is appropriate for their use. If it is part of the class' interface, it should | + | This is the best way to manage your files, and the way I will use in the remainder of these tutorials. However, depending on how you are expected to submit homework, you may be required to use the "easy for beginners" |
- | </ | + | |
Copyright © 2020 Mithat Konar. All rights reserved. | Copyright © 2020 Mithat Konar. All rights reserved. |
java/defining_classes_in_java.txt · Last modified: 2020/09/14 01:38 by mithat