java:defining_classes_in_java
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext 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:05] – [Where does the definition go?] 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 '' | + | This definition states that an instance of a '' |
- | ==== Where does the definition | + | ==== Instantiation and use ==== |
+ | |||
+ | The above is just a class definition. It doesn' | ||
+ | |||
+ | |||
+ | <code java> | ||
+ | var myCounter = new ClickerCounter(); | ||
+ | |||
+ | myCounter.reset(); | ||
+ | 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 === | + | ==== The easy-for-beginners way ==== |
If there' | If there' | ||
<file java ClickerCounter.java> | <file java ClickerCounter.java> | ||
- | tbd | + | public class Counter { |
+ | |||
+ | // Class definition | ||
+ | int count; | ||
+ | |||
+ | void click(){ | ||
+ | count++; | ||
+ | } | ||
+ | |||
+ | void reset(){ | ||
+ | count = 0; | ||
+ | } | ||
+ | |||
+ | // Program' | ||
+ | public static void main(String[] args) { | ||
+ | var myCounter = new ClickerCounter(); | ||
+ | |||
+ | myCounter.reset(); | ||
+ | myCounter.click(); | ||
+ | System.out.println(myCounter.count); | ||
+ | myCounter.click(); | ||
+ | myCounter.click(); | ||
+ | System.out.println(myCounter.count); | ||
+ | myCounter.reset(); | ||
+ | System.out.println(myCounter.count); | ||
+ | } | ||
+ | } | ||
</ | </ | ||
This isn't generally recommended though because very often you will write more than one custom class in your program. | This isn't generally recommended though because very often you will write more than one custom class in your program. | ||
- | === The better way === | + | ==== The better way ==== |
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 '' | 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 '' | ||
Line 86: | Line 130: | ||
</ | </ | ||
- | |||
- | ===== Access specifiers ===== | ||
- | The '' | ||
- | |||
- | <file java ClickerExample.java> | ||
- | public class ClickerExample { | ||
- | |||
- | public static void main(String[] args) { | ||
- | var myCounter = new ClickerCounter(); | ||
- | | ||
- | myCounter.reset(); | ||
- | myCounter.count = 492341; | ||
- | System.out.println(myCounter.count); | ||
- | myCounter.click(); | ||
- | myCounter.click(); | ||
- | System.out.println(myCounter.count); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | 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 class ClickerCounter { | ||
- | | ||
- | private int count; | ||
- | | ||
- | public void click(){ | ||
- | count++; | ||
- | } | ||
- | | ||
- | public void reset(){ | ||
- | 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 ' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | * '' | ||
- | |||
- | With the changes made above, if we try to run '' | ||
- | |||
- | <code java> | ||
- | myCounter.count = 492341; | ||
- | </ | ||
- | |||
- | it cannot access the '' | ||
- | |||
- | <WRAP center round tip 80%> | ||
- | 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 '' | ||
- | </ | ||
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