Java Objects and Their Properties

A typical object-oriented program consists of a set of interacting objects. Each object has its own state separated from others. Each object is an instance of a particular class (type) that defines common properties and possible behavior for its objects.

All classes from the standard library (StringDate) and classes defined by programmers are reference types which means that variables of these types store addresses where the actual objects are located. In this regard, the comparison and assignment operations work with objects differently than with primitive types.

Creating objects

The new keyword creates an object of a particular class. In this block of code, we create a standard string and assign it to the variable str:

String str = new String("hello");

The variable str stores a reference to the String object "hello" located somewhere in the heap memory.

In the same way, we can create an object of any class we know.

Here is a class that describes a patient in a hospital information system:

class Patient {
    String name;
    int age;
}

Here is an instance of this class:

Patient patient = new Patient();

Despite the fact that String is a standard class and Patient is our own class, both classes are regular reference types. However, there is a big difference between those classes and we will discuss it below.

Immutability of objects

There is an important basic concept in programming called immutability. Immutability means that an object always stores the same values. If we need to modify these values, we should create a new object. A common example is the standard String class. Strings are immutable objects so all string operations produce a new string. Immutable types allow you to write with fewer errors. In the context of application development, immutability can improve reliability and provide more maintainable code.

The class Patient is not immutable (i.e. the class is mutable) because it is possible to change any field of an object.

Patient patient = new Patient();

patient.name = "Mary";
patient.name = "Alice";

Sharing references

More than one variable can refer to the same object, check out this code block:

Patient patient = new Patient();

patient.name = "Mary";
patient.age = 24;

System.out.println(patient.name + " " + patient.age); // Mary 24

Patient p = patient;

System.out.println(p.name + " " + p.age); // Mary 24

It is important to understand that two variables refer to the same data in memory rather than two independent copies. Since our class is mutable, we can modify the object using both references.

patient.age = 25;
System.out.println(p.age); // 25

Nullability

As for any reference type, a variable of a class type can be null which means it is not initialized yet.

Patient patient = null;

This is a common feature in Java available for classes since they are reference types.

Attempting to access a reference that points to null will result in an exception. It indicates that you're trying to perform an operation on a non-existent object. We'll deal with this topic in the following topics.

Conclusion

Java is an object-oriented programming language, so the concept of objects that can model real-world entities is one of its fundamental concepts.

By now, not only have we already worked with some classes from the standard library but also learned how Java allows us to create our own classes. In this topic, we've discussed that the nature of custom classes' objects and standard library ones are based on the same principles.

Keep in mind, that Java classes defined by programmers are reference types. When objects are created using the new operator, it returns a reference in memory where the created objects are located. With this reference, we can get access to fields and change them. Several variables can refer to the same object through a reference. It is also possible to create two independent objects with the same field's content. It's important to understand that references to such objects are different. However, not all objects allow changing their state after creation. Such a feature is called immutability, which is also one of the core concepts in Java programming.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate