top of page

Week 7

In Week 7 you will learn about classes and objects and how they can be used to enhance your programs.



A class in Java allows us to implement an algorithm, a program, a library with functionality for other programs to use and also to implement a type. Until now we mostly have used classes in the form of implementing a very simple program or algorithm or have seen it in the form of libraries like Random, Scanner and PrintStream. When implementing an algorithm we mostly write some static methods and then call these from within the main method. We will now see how to expand on this allow us to implement much more complex classes.



public class className {
    //member variables
    //member methods

A class, at least for now, should be declared using the keywords public and class followed by the class name. Within the class one can find variables and methods which we call member variables and class methods. Member variables give the state of the class and the methods give the behaviour of the class.

All non-primitive types in Java are defined in a class carrying the same name as the type. In this class we define the types behaviour and once an instance of this class (an object) is created we can store the state of the object within the member variables.

Objects are instances of a class and many instances of one class can exist all with a different state. Any non-static methods of a class can be used only after an object of said class has been created.




When creating a class we must define how to create an instance of it. We use constructors to do so. Constructors are method named like the class they are defined inside. We declare constructors with the public keyword. When no constructors are implemented, Java will automatically generate a default constructor in the background that will set all member variables to their default type. Member variables can also be reference variables which will then be set to null.

If any constructor is defined the default constructor is not automatically created anymore and we must create it ourselves. The code to the left defines a type "Point" which we can now create instances of using "new Point()" or "new Point(x, y)".


String Constructor

String s1 = "Hello";
String s2 = new String("Hello");

One may now ask that if Strings are objects then why do we not use a constructor to create them. Strings also have a constructor and we can, but should not, use it. This is because using the quotation mark initialisation will attempt to not create a new object for each String and instead if a String has already been created once the same object is reused. This is possible because Strings are immutable, i.e. once an object has been created it cannot be changed.

This non-guarantee that two identical strings are not stored in the same object forces us to always use the equals method inside the String class to compare two Strings instead of using ==. The operator == will only check if the references inside the two reference variables are equal.


Arrays are Objects

int[] intArr = new int[4];
String[] strArr = new String[10];

Arrays are objects too. One can tell that this is true as we use the new keyword when initialising it which is only used for objects. We can call methods on array instances and also access their member variables. These are the distinctive features of an object. A variable for a array is a reference variable and contains a reference to the array object in memory.




All operators defined for primitive types do not work on objects, or at least do not work as they do for primitive types. The + operator works on Strings but on no other object. The == operator works on the references stored within the reference variables but not the objects themselves, as we use the equals method to compare objects. All other operators are not defined and can only be used on primitive type variables and values.

The code on the left or something similar might work in other programming languages but it does not in Java. We define methods for objects which take the place of operators for primitive types.

Arrays Class



The Arrays class offers a lot of utility which one may want to use during writing a program. We can create a copy of an array using Arrays.copyOf and retrieve the array as a String using Arrays.toString. Creating copies of objects is often important as simply setting one reference variable to another will have both references pointing to the same object. If we want to make changes to a copy rather than to the original object which often is required then we need to be able to create exact copies without having to write a method ourselves.

Be careful however, if we have an Array of an Object-type then the objects themselves are not copied as well and only the reference itself is copied. Hence if we want to create a real copy with no overlap in terms of references we cannot use the copyOf method. 

Objects as Parameters



Objects like primitive types values can be used as parameter of a method. We declare object parameters as we do with primitive type parameters.

We have seen that Object parameters follow reference semantics rather than value semantics (which primitive types do). This is important to remember. Whenever a Object is passed to a method we give the method a copy of the reference to the Object hence we can still make changes to the underlying Object. With primitive types we copy the value stored in the variable and hence cannot make changes to the underlying Object.

As can be seen to the left Objects can also be the return type of a method.

bottom of page