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:04] – 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 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, '' |
- | ==== Instantiation and use ==== | + | 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. |
- | The above is just a class definition. It doesn' | + | +==== Instantiation and use ====+ |
- | The program | + | The above is just a class definition. It doesn' |
- | <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 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 " |
- | 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, | + | |
<file java ClickerCounter.java> | <file java ClickerCounter.java> | ||
- | public class ClickerCounter | + | public class Counter |
+ | |||
+ | // Class definition | ||
+ | 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