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:04] – mithat | java:defining_classes_in_java [2020/09/12 03:04] – 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 '' | + | This definition states that an instance |
- | + | ||
- | In the above, | + | |
==== 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 '' | ||
- | <file java ClickerExample.java> | + | <code java> |
- | public class ClickerExample | + | 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 definition go? ===== | ||
+ | |||
+ | ==== The easy-for-beginners way ==== | ||
+ | If there' | ||
+ | |||
+ | <file java ClickerCounter.java> | ||
+ | public class Counter | ||
+ | |||
+ | // Class definition | ||
+ | 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 98: | ||
</ | </ | ||
+ | This isn't generally recommended though because very often you will write more than one custom class in your program. | ||
- | ===== Access specifiers ===== | + | ==== The better way ==== |
- | 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 '' | ||
+ | |||
+ | In the above, '' | ||
+ | |||
+ | ==== Instantiation and use ==== | ||
+ | |||
+ | The above is just a class definition. It doesn' | ||
+ | |||
+ | The program below shows you how to make, or **instantiate** a '' | ||
<file java ClickerExample.java> | <file java ClickerExample.java> | ||
Line 79: | Line 116: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
- | var myCounter = new ClickerCounter(); | + | var myCounter = new ClickerCounter(); |
| | ||
- | myCounter.reset(); | + | myCounter.reset(); |
- | myCounter.count = 492341; // <- LOOK HERE! | + | myCounter.click(); // count is 1 |
System.out.println(myCounter.count); | System.out.println(myCounter.count); | ||
- | myCounter.click(); | + | myCounter.click(); |
- | myCounter.click(); | + | myCounter.click(); |
+ | System.out.println(myCounter.count); | ||
+ | myCounter.reset(); | ||
System.out.println(myCounter.count); | System.out.println(myCounter.count); | ||
} | } | ||
Line 91: | Line 130: | ||
</ | </ | ||
- | 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