Creating and Storing Objects (Instantiation)

Creating and Storing Objects (Instantiation)

Understanding Object Instantiation

  • Object-oriented programming revolves around objects and their interactions.
  • An object, in programming, is an instance of a class with state and behaviour, defined by attributes and methods respectively.
  • The process of creating an object is referred to as instantiation.

Creating an Object

  • In Java, an object is instantiated through the new keyword.
  • The general syntax to instantiate an object is ClassName objectName = new ClassName();
  • Each time the new keyword is used, a new object is created in memory.
  • After the object is created, you can use the dot operator (.) to access the object’s methods and attributes defined in its class.
  • For instance: If we have a class Dog, an instance (object) of Dog can be created as Dog myDog = new Dog();

Storing an Object

  • Variables of a class type (like Dog in our example) can reference any object of that class or any object from a class that subclasses the class type of the variable.
  • When an object is stored in a variable, the variable does not actually contain the object. Instead, it contains a reference to the object.
  • This means that the variable points to the location in memory where the object is stored.
  • If the object is no longer needed, you can set the variable that references it to null. Once no references to an object remain, the Java garbage collector can reclaim the memory.

Constructors

  • When an object is instantiated, a special method called a constructor is called.
  • The constructor has the same name as the class and initializes the state of a new object.
  • If no constructor is provided in the class definition, a default (no-argument) constructor is implicitly provided.
  • A class can have multiple constructors defined, each having different parameter lists. This is known as constructor overloading.
  • Constructors do not have a return type and are called using the new keyword.

Static vs. Instance

  • Instance variables and methods are associated with an instance of a class. Each time an object is created, a new set of instance variables is created.
  • Static variables and methods, on the other hand, belong to the class, not any particular instance. They are shared by all instances of the class.
  • You can call a static method without creating an object of the class, by using the class name, e.g. ClassName.methodName(). Conversely, to call an instance method, you must have an instance (object) of the class.
  • The key distinction between static and instance relates to data sharing. Static means shared across all instances while instance means unique to each object.

Object References

  • It is important to be aware that when you assign an object variable to another, you are copying the reference, not the actual object. Both variables end up referring to the same object.
  • Changes made to the object through one variable will be reflected when the object is accessed through the other variable.

Wrapping Up

  • Understanding how to create and store objects is a fundamental part of object-oriented programming. Each object carries its own state and behaviour, making it easier to design and build complex systems.