User Tools

Site Tools


java:defining_classes_in_java

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java:defining_classes_in_java [2020/09/12 03:05] – [Where does the definition go?] mithatjava:defining_classes_in_java [2020/09/14 01:38] (current) – [Our first Java class] mithat
Line 43: Line 43:
 </code> </code>
  
-This definition states that an instance of a ''ClickerCounter'' will consist of a ''count'' member variable, a method ''click()'' that increments the count, and a method ''reset()'' that resets the count.+I have deliberately not used any comments in the definition so you can more easily see the code.
  
-==== Instantiation and use ====+In the above, ''count'' is an attribute/instance variable that keeps track of the state of the object. and the ''click'' and ''reset'' methods implement the behavior.
  
-The above is just a class definition. It doesn't actually make an object you can useThe code below shows you how to make, or **instantiate** a ''ClickerCounter'' and then call the object'methods to change the state of the object+In actuality, this is a pretty crappy class definition for reasons we'll see laterBut for now it'enough to get us started. 
 + 
 +==== Instantiation and use ====
  
 +The above is just a class definition. It doesn't actually make any objects you can use. The code below shows you how to make, or **instantiate** a ''ClickerCounter'' object and then call the object's methods to change the state of the object. 
  
 <code java> <code java>
Line 66: Line 69:
  
 ==== The easy-for-beginners way ==== ==== The easy-for-beginners way ====
-If there's only one custom class in your program, an easy way to write your program is to incorporate the definition and the program's ''main()'' method.+If there's only one custom class in your program, an easy way to write your program is to incorporate the class definition and the program's ''main()'' method into the same class definition.
  
 <file java ClickerCounter.java> <file java ClickerCounter.java>
-public class Counter {+public class ClickerCounter {
  
-    // Class definition+    // Class definition (instance members)
     int count;     int count;
          
Line 98: Line 101:
 </file> </file>
  
-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. 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.))
  
 ==== The better way ==== ==== The better way ====
  
-In Java, class definitions need to go in their own filesand the files need to be called the name of the class with the ''.java'' extension.((There are certain kinds of classes that don't need to be in their own file, but that's something we can't discuss yet.)) I have deliberately not used any comments in the definition so you can more easily see the code.+A better way to manage your programs' custom classes is to put each custom class into its own file. When you do this, the files need to be the name of the class with the ''.java'' extension.
  
-In the above, ''count'' is an attribute/instance variable that keeps track of the state of the object. the ''click'' and ''reset'' methods implement the behavior. 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 following is how to break up the class definition and the "main" stuffHere is the file that has only the class definition:
  
-==== Instantiation and use ====+<file java ClickerCounter.java> 
 +public class Counter {
  
-The above is just a class definition. It doesn't actually make an object we can use.+    // Class definition 
 +    int count; 
 +     
 +    void click(){ 
 +        count++; 
 +    } 
 +     
 +    void reset(){ 
 +        count = 0; 
 +    } 
 +
 +</file>
  
-The program below shows you how to make, or **instantiate** a ''ClickerCounter'' and then call the object's methods to change the state of the object.+And here is the file that has the "main" stuff:
  
 <file java ClickerExample.java> <file java ClickerExample.java>
Line 130: Line 145:
 </file> </file>
  
 +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" way to write you code.
  
 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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki