Java Class: Using Classes and Objects in Java

In this tutorial we're going to take a look at classes and objects in Java. If you need a refresher on packages in Java, with help on compiling and running simple Java programs, take a look here.



Declaring a Class



package caveofprogramming.aquarium;

public class Fish {
    public static void main(String [] args) {
        System.out.println("Hello");
    }
}




Here we've declared a minimal class, with a static main() method so that it can run as a little self-contained program.

The class has been declared public, meaning that all classes can access the class from all packages. If you have a main() in your class, you must make the class public.

Usually, however, you will declare classes in java without specifying a modifier.

A More Complex Example



Here's a class with a single method, speak().

Food.java:

package caveofprogramming.aquarium;

class Food {
    public void speak() {
        System.out.println("Eat me!");
    }
}





Now we will make Fish.java use Food.java. We'll make the code in Fish.java create an object (in other words, a particular instance) of the Food class and call the speak() method that we've defined.

package caveofprogramming.aquarium;

public class Fish {
    public static void main(String [] args) {
        Food food = new Food();
        food.speak();
    }
}




C:John>java caveofprogramming.aquarium.Fish
Eat me!




Here we've declared the speak method to be public. There are other possibilities.


  • public: accessible from all other classes, everywhere.

  • protected: accessible only from within the same package.

  • private: accessible only from within the same class.



Where possible use private or protected; expose as little of your code as possible.

Constructors



A constructor is a method that's called when an object of your class is created. The constructor simply has the same name as your class.

In the following example, the constructor outputs a message while the main() does nothing other than create an instance of the class.

package caveofprogramming.aquarium;

public class Fish {

    // Constructor
    protected Fish() {
        System.out.println("Constructor called!");
    }
    
    public static void main(String [] args) {
        Fish fish = new Fish();
    }
}




C:work>java caveofprogramming.aquarium.Fish
Constructor called!




Instance Variables



Of course your class can contain data as well as methods. Variables can be public, protected or private just like methods. As with methods, you should make them as private as possible, within reason.

You can also have static data members, which are shared between all objects of the class.

The following example illustrates all of these ideas, as well as demonstrating a constructor that takes a parameter.

Also, notice that we use this to refer unambiguously to an object's instance variables.

package caveofprogramming.aquarium;

public class Fish {

    // static variables are shared by all instances
    // of the class.
    private static Integer count = new Integer(0);

    // Non-static variables are specific to the object.
    protected String name;
    protected Integer number;

    // Constructor
    protected Fish(String name) {
        // Set name to whatever's passed in.
        this.name = name;       

        // Set number to whatever the count
        // is at the moment.
        this.number = count;

        // Now increment count.
        // count will then be a count of the number
        // of objects created of the class.
        count++;
    }

    // Show some stuff about the class.
    protected void show() {
        System.out.println(number.toString() + 
            ". " + this.name);
    }

    // A main method just to run the whole thing.
    public static void main(String [] args) {
        Fish fish1 = new Fish("Lisa");
        Fish fish2 = new Fish("Pete");
        Fish fish3 = new Fish("Richard");

        fish1.show();
        fish2.show();
        fish3.show();
    }
}




0. Lisa
1. Pete
2. Richard




Conclusion



Classes and objects are a big subject. We haven't touched here on inheritance or interfaces, for instance. But there is enough code here to enable you to create and use basic classes, or to enable you to make use of classes if you've forgotten the exact syntax and just needed a refresher.

If there's anything else you'd particularly like to see covered in this article, feel free to leave a comment.