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 03:05] – [Where does the definition go?] mithat | java:defining_classes_in_java [2020/09/14 01:38] (current) – [Our first Java class] mithat | ||
---|---|---|---|
Line 43: | Line 43: | ||
</ | </ | ||
- | This definition states that an instance of a '' | + | I have deliberately not used any comments in the definition so you can more easily see the code. |
- | ==== Instantiation | + | In the above, '' |
- | The above is just a class definition. It doesn't actually make an object you can use. The code below shows you how to make, or **instantiate** a '' | + | In actuality, this is a pretty crappy |
+ | |||
+ | ==== Instantiation and use ==== | ||
+ | The above is just a class definition. It doesn' | ||
<code java> | <code java> | ||
Line 66: | Line 69: | ||
==== The easy-for-beginners way ==== | ==== The easy-for-beginners way ==== | ||
- | If there' | + | If there' |
<file java ClickerCounter.java> | <file java ClickerCounter.java> | ||
- | public class Counter | + | public class ClickerCounter |
- | // Class definition | + | // Class definition |
int count; | int count; | ||
| | ||
Line 98: | Line 101: | ||
</ | </ | ||
- | 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 | + | A better way to manage your programs' |
- | In the above, '' | + | The following |
- | ==== Instantiation and use ==== | + | <file java ClickerCounter.java> |
+ | public class Counter { | ||
- | The above is just a class definition. It doesn' | + | // Class definition |
+ | int count; | ||
+ | |||
+ | void click(){ | ||
+ | count++; | ||
+ | } | ||
+ | |||
+ | void reset(){ | ||
+ | count = 0; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
- | The program below shows you how to make, or **instantiate** a '' | + | And here is the file that has the " |
<file java ClickerExample.java> | <file java ClickerExample.java> | ||
Line 130: | Line 145: | ||
</ | </ | ||
+ | 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.1599879921.txt.gz · Last modified: 2020/09/12 03:05 by mithat