Unit 2.1- Objects: Instances of Classes

What are objects?

An Object is created from a class by calling the class constructor.

What are classes?

A Class is a template that defines an object is like and what it can do.

Objects are Instances of Classes

For example, if the class is a blueprint for a house, the object is a particular house.

Example

Here is an example of creating a Student class in Java. It contains instance variables that define the characteristics of a student.

public class Student {

    // instance variables
    String name;
    String teacher;
    int period;
  
    // constructor
    public Student(String name, String teacher, int period) {
      this.name = name;
      this.teacher = teacher;
      this.period = period;
    }
  }

Now, using our class, we can create student objects by initializing two students

Student grace = new Student("Grace", "Mr. Mort", 3);
Student alice = new Student("Alice", "Mr. Mort", 1);

We can printout information about each student

System.out.println(grace.name);
Grace
System.out.println(alice.period);
1

Learning Objectives

  • Explain the relationship between a class and an object.

Essential Knowledge

  • An object is a specific instance of a class with defined attributes.
  • A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.

Unit 2.2- Creating and Storing Objects (Instantiation)

So, after we learned about classes, how are objects actually made?

Using our previous code, we can see created our constructor using

public Student(String name, String teacher, int period) {
      this.name = name;
      this.teacher = teacher;
      this.period = period;
    }
public class Student {

    // instance variables
    String name;
    String teacher;
    int period;
  
    // constructor
    public Student(String name, String teacher, int period) {
      this.name = name;
      this.teacher = teacher;
      this.period = period;
    }
  }

The Student is the name of the class. The first letter is capitalized according to Java naming conventions (camel-case naming conventions). Then, using the new keyword, we call the constructor to make a new Student. Inside the parentheses, we have the parameter list, where the values and characteristics of the object are entered.

Student grace = new Student("Grace", "Mr. Mort", 3);

The parameters in this case are “Grace”, “Mr. Mort”, and 3.

Constructor Overloads

A class can have multiple constructors, however, the number of parameters must be different or the order must be different. This is an example of overloading the constructor.

// Constructor 1
Student(String name, String teacher, int period)


// Constructor 2
Student(String teacher, String name, int period)

This two constructors are not allowed. For example, if we call

Student("Grace", "Mr. Mort", 3);

we would not know whether Grace or Mr. Mort is the student.

However, constructors with different data types or different number of parameters are allowed when overloading. Here are some examples…

Student(String name, String teacher, int period)
Student(String name, String teacher)
Student()

Null Objects

Student rachit = null;

Null basically states that no object exists, and it is not stored in memory. You cannot call methods on an object that is declared as null since null objects do not have any information or characteristics set to that object. This will create a NullPointerException

Unit 2.3- Calling a Void Method

method: code that is called in order to achieve a task

  • can be void or non-void, static or non-static

void method: do not return a value but instead change other things. These include changing characteristics of an object or printing text to the console. Here is an example…

public void methodName(parameterList)

static method: general to the class and not tied to any particular object. The method is denoted by the Here is an example …

public static void add() {
  count++;
}

To call a static method, we use dot notation, with the class name coming before the method separated by a dot as follows

ClassName.add();

non-static method: acts on a particular object. For example, printing a person’s name is a non-static method, since each person has a different name. For example…

public void printName() {
  System.out.println(name);
}

When calling a non-static method, we also use dot notation. But, instead of using the class name, we use the object name so we know what object the method acts on. Also, we don’t need to do dot notation of ClassName.objectName.methodName() because an object already acts on a certain class, so using the class name is just redundant. We would use printName() as follows…

objectName.printName();

Run the main method and see what the each of the methods prints out

public class MyClass {

    // Static method
    public static void staticMethod() {
        System.out.println("This is a static method.");
    }

    // Non-static (instance) method
    public void nonStaticMethod() {
        System.out.println("This is a non-static method.");
    }

    // Method with void return type
    public void methodWithVoidReturnType() {
        System.out.println("This method has a void return type.");
    }

    public static void main(String[] args) {
        // Calling the static method
        staticMethod();

        // Creating an object of MyClass
        MyClass myObject = new MyClass();

        // Calling the non-static method on the object
        myObject.nonStaticMethod();

        // Calling the method with void return type
        myObject.methodWithVoidReturnType();
    }
}