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 02:49] – [Our first Java class] 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 43: | Line 43: | ||
</ | </ | ||
- | ==== Where does the definition | + | I have deliberately not used any comments in the definition |
+ | In the above, '' | ||
- | 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 actuality, this is a pretty crappy class definition for reasons we'll see later. But for now it's enough to get us started. |
- | + | ||
- | 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 '' | + | <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 74: | 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.1599878979.txt.gz · Last modified: 2020/09/12 02:49 by mithat