User Tools

Site Tools


java:defining_classes_in_java

This is an old revision of the document!


Defining Classes in Java

We are going to write a simple class for implementing a counter similar to one of these:1)

In case you've never seen one of these bits of advanced technology before, it's a tally counter or, as I prefer to call it, a clicker counter. It has two controls: a button on top you click to advance the counter by one and another button elsewhere you press to reset the count to zero. Our goal is to build one of these in software using object-orientation.

We first have to decide what a counter thing is. One way to start building this model for a class is to start listing the public-facing behavior (or the interface) you want an object of that class to have. A pretty comprehensive list of the things you might do with a clicker-counter is:

  • click: makes the count increase by one.
  • reset: sets the count to zero.

Next we can think about what attributes we'll need to keep track of the state of a clicker-counter. In this case, it's pretty simple: all we really need is one integer to store the count value.

So, a summary of what we need so far is:

  • a click operation
  • a reset operation
  • an integer to store the count

In Java, object attributes, which together make up the state, are defined in variables called instance variables. A class definition can include as many instance variables as it needs to store the state. In our case, we are getting off easy: the clicker-counter only needs one. The operations our object will be capable of, which make up behavior, are defined using methods. We call anything belonging to a class (e.g., instance variables and methods) a member of the class.

Our first Java class

Class names in Java traditionally use CamelCase with the first letter capitalized.

Here's a Java definition for a ClickerCounter class:

public class ClickerCounter {
 
    int count;
 
    void click(){
        count++;
    }
 
    void reset(){
        count = 0;
    }    
}

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.

Instantiation and use

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 ClickerCounter and then call the object's methods to change the state of the object.

var myCounter = new ClickerCounter();  // instantiate a ClickerCounter
 
myCounter.reset();  // count is 0
myCounter.click();  // count is 1
System.out.println(myCounter.count);
myCounter.click();  // count is 2
myCounter.click();  // count is 3
System.out.println(myCounter.count);
myCounter.reset();  // count is 0
System.out.println(myCounter.count);

Where does the code go?

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.

ClickerCounter.java
public class Counter {
 
    // Class definition
    int count;
 
    void click(){
        count++;
    }
 
    void reset(){
        count = 0;
    }   
 
    // Program's main method that uses the class
    public static void main(String[] args) {
        var myCounter = new ClickerCounter();  // instantiate a ClickerCounter
 
        myCounter.reset();  // count is 0
        myCounter.click();  // count is 1
        System.out.println(myCounter.count);
        myCounter.click();  // count is 2
        myCounter.click();  // count is 3
        System.out.println(myCounter.count);
        myCounter.reset();  // count is 0
        System.out.println(myCounter.count);
    }    
}

This isn't generally recommended though because very often you will write more than one custom class in your program.

The better way

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 .java extension.2) I have deliberately not used any comments in the definition so you can more easily see the code.

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

Instantiation and use

The above is just a class definition. It doesn't actually make an object we can use.

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.

ClickerExample.java
public class ClickerExample {
 
    public static void main(String[] args) {
        var myCounter = new ClickerCounter();  // instantiate a ClickerCounter
 
        myCounter.reset();  // count is 0
        myCounter.click();  // count is 1
        System.out.println(myCounter.count);
        myCounter.click();  // count is 2
        myCounter.click();  // count is 3
        System.out.println(myCounter.count);
        myCounter.reset();  // count is 0
        System.out.println(myCounter.count);
    }    
}

Copyright © 2020 Mithat Konar. All rights reserved.

1)
Picture from: “Totty Clicker - Gadgets at Play.com (UK).” Play.com (UK): DVDs, Music CDs, MP3s, Video Games, Books, Electronics & Gadgets - Free Delivery. http://www.play.com/Gadgets/Gadgets/4-/11566684/Totty-Clicker/Product.html?ptsl=1&ob=Price&fb=0# (accessed January 25, 2011).
2)
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.
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