Java Lesson 04 – Classes


A big part of learning how to program, is knowing how to use and create classes. So far we have already seen a couple of classes: System and Scanner. These are all classes included with java. Things that weren’t classes were int and double. Those are primitives.

Classes in general start with an Uppercase and each subsequent word is also capitalized, example: InputStreamReader. When you initialize a class you always use the new keyword: Scanner scanned = new Scanner(). Primitives on the other hand can be directly initilized: int i = 10;

Now let’s create our first class. Create a new project and call it Lesson 04. In the project right click the src folder -> New -> Class -> name it Animal (make sure public static void main(String[] args) is unchecked) -> press Finish. An should have been created now with the following code inside:

public class Animal {


The keyword class is used to create a class and public means that it can be used by other classes, it’s publicly available. The name of the class is Animal and should be the same as your .java file. Everything between the {} will be part of your class. Something that you will also need to know is that all Class inherit the Class Object. This means that all classes are of the Object type.

Next just like the previous lessons create a Main class where you initialize our Animal class:

public class Main {

	public static void main(String[] args) {
		Animal horse = new Animal();


In this line the Animal horse = is where you create a new Object called horse with the class type Animal. With = new Animal(); you initialize your Animal Object called horse with the class Animal. So to initiate a new class the syntax is: <class type> <initialized object name> = new <class type>(); . What the round brackets are for at the end we will go over later.

When you run this it will print out something like: Animal@e53108, which is basically <class>@<hash>. The hash is used by the compiler and you will probably never have to do anything with that. This print out doesn’t really tell us enough. We want it to print out that its a horse, but so far our Animal class doesnt know its a horse yet.

So lets first create two functions: One to set the type of the Animal class and One to get the Type.

Lets first create the function that will set the type of our Animal.

	public void setType(String type){


Here we created a function called setType which takes one parameter that is of the type String.

Next create a function that will get the type from our Animal.

	public String getType(){


The getType doesnt need any parameters, but it does have a return type and it’s a of the type String. If a class doesnt have a return type it will be called void, like the setType function.The syntax to create function is basically: <plublic/private> <return type> <function name>(<parameters>){}

The getType function is probably also give you an error, because it has a return type defined but its not returning anything, we will fix that next.

To actually make the class remember it’s type we will need to create a class variable. At the top of you class (inside the {} ) place:

	private String type = "None";

So if your Animal object doesnt have a type set, it’s default type is None. To make the setType function set the class variable instance add: this.type = type; to it. To make the getType return the type add return type; to it.

Result SelectShow

The this. in the setType is to reference the class variable. In  the setType function there are two variables with the type name, the one that is given when the function is called and the class variable. When you put your cursor on the type variable it should highlight where it’s being used. The keyword this is used to call the current instance of the object and call its variables or functions, it’s not always necessary to use, but to make the distinction between the class variable and the one given it is in our case.

Now to change what the Syste.out.println prints out we will need to override the toString function of the Object class which all class inherit. This function has a String as return type. In this String we are going to return our type.

	public String toString(){
		return "Animal type: " + getType();

Above this function you see @Override. This is an annotation. This annotation is used to tell the compiler is overriding a function that it inherited from another class. Basically this means that it will just throw an error if it’s trying to override a function that doesn’t exist yet. Try change the function name to tooString for example. It will give you an error, because that’s a function it doesn’t know. The same thing if you try to change the return type to something else.

Now when you run the following code in your Main class:

		Animal horse = new Animal();

		Animal animal = new Animal();

It should print out: Animal type= Horse and Animal type= None.


The assignment is to add a name and weight to the Animal. So create a setName and getName, the name is a string. Also create a setWeight and getWeight and weight is a double. Finally change the toString function to also display the name and weight of the animal. To test if everything is working create 10 animals with different values.


The examples and the solutions to the Assignment you can download here

Special note

A String is also a class, not a primitive, even though you don’t need to use new to initialize it. String is an odd one in this. Because String is a simple class and it’s used a lot, java added a way to optimize memory use. When you create two different strings with the same value, they will have the same reference in your memory. You can call new String as well to give a new reference, that is why when comparing Strings you should always use equals, which checks if the values are the same.

		String a = "abc";
		String b = "abc";
		String c = new String("abc");
		String d = new String("abc");

		System.out.println(a == b); //gives True
		System.out.println(c == d); //gives False
		System.out.println(c.equals(d)); //gives True