OOP1-#Part1|Java Programming: Understanding Classes, Objects, and Memory Allocation — The Three Essential Properties of Java Objects, the Dot Operator, Dynamic Memory Allocation, Constructors, Object References vs. Object Copies, and Method Parameters and Arguments

Nikita pandey
6 min readFeb 18, 2023

Sharing my notes here because why not!

1. Classes and Objects

In object-oriented programming, a class is a template or blueprint for creating objects. An object is an instance of a class, and it has physical existence in memory. When you declare an object of a class, you are creating an instance of that class.

A class creates a new data type that can be used to create objects. Objects are characterized by three essential properties: state, identity, and behavior. The state of an object is a value from its data type. The identity of an object distinguishes one object from another, and it is the place where its value is stored in memory. The behavior of an object is the effect of data-type operations.

Questions->>

what does the instance of a class mean?

In object-oriented programming, a class is a template or blueprint for creating objects. It defines the properties and methods that the objects created from that class will have.

When you declare an object of a class, you are creating an instance of that class. In other words, you are creating a specific object that has the properties and methods defined by the class. The object that you create is an instance of the class.

For example, let’s say you have a class called “Car” that defines the properties and methods that all cars should have. You can then create specific cars by declaring objects of the “Car” class. Each car object that you create is an instance of the “Car” class and has the properties and methods defined by that class.

Here’s an example of creating an object of the “Car” class in Java:

Car myCar = new Car();

In this example, “myCar” is an object of the “Car” class, which means it is an instance of the class. It has the properties and methods defined by the “Car” class, which allows you to do things like set the car’s color or drive it.

In summary, an object is an instance of a class, which means it is a specific, concrete implementation of the properties and methods defined by that class.

Explain, “Objects are characterized by three essential properties: state, identity, and behavior.”

In object-oriented programming, a class is a blueprint for creating objects of a certain type. It defines the properties and behaviors that objects of that type will have. When you create an object from a class, you are creating an instance of that class. Each instance of a class has its own state, identity, and behavior.

The state of an object refers to the values of its instance variables at a given point in time. An instance variable is a variable defined in a class that holds a value specific to an instance of the class. For example, if you have a class called “Car” with instance variables for make, model, and year, then the state of an instance of the “Car” class would be the specific values of those variables for that particular car.

The identity of an object distinguishes one object from another. It is the location in memory where the object is stored. Each object has a unique identity that is assigned when it is created, and this identity remains constant throughout the object’s lifetime. Two objects of the same class can have different states and identities, and they can be distinguished from each other based on their identities.

The behavior of an object is defined by its methods, which are the functions that can be called on the object. A method is a collection of statements that perform a specific task. For example, a “Car” class might have a method called “start” that starts the engine of the car. When you call the “start” method on an instance of the “Car” class, it will perform the necessary actions to start the engine for that particular car.

In summary, a class creates a new data type that can be used to create objects. Objects have state, identity, and behavior. The state of an object is the value of its instance variables at a given point in time, the identity of an object is its location in memory, and the behavior of an object is defined by its methods.

Photo by Christopher Gower on Unsplash

2. Dot Operator and new Keyword

The dot operator links the name of the object with the name of an instance variable. Although commonly referred to as the dot operator, the formal specification for Java categorizes the “.” as a separator.

The “new” keyword dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it. This reference is the address in memory of the object allocated by the new. All class objects in Java must be dynamically allocated.

Questions->>

What does it mean by instance variable?

In object-oriented programming, an instance variable is a variable defined in a class but outside of any method, constructor, or block. Also known as member variables or attributes, instance variables are associated with objects of the class and are used to represent the state of the object.

When you create an object of a class, the object has its own set of values for each of its instance variables. For example, in a “Person” class, each instance of a Person object might have instance variables like “name”, “age”, “height”, and “weight”. Each person object would have its own values for these instance variables.

Instance variables can have different access modifiers such as public, private, protected, or package-private, which determine the level of access to the variable from outside the class.

You can access instance variables using dot notation by specifying the object name followed by a dot (.) and the name of the variable. For example, if you have an instance variable “name” in a Person object named “person1”, you can access it using the expression “person1.name”.

3. Creating Objects with new

To create an object of a class in Java, you can use the following syntax:

ClassName objectName = new ClassName();//ClassName()is a constructor

Here, “objectName” is a variable of the class type being created, and “ClassName” is the name of the class being instantiated. The “new” keyword is used to allocate memory for the object and to call the constructor of the class.

4. Object References

When you create an object using the new keyword, you are creating a reference to that object. A reference is a variable that holds the memory address of the object.

For example, consider the following code:

Box b1 = new Box();
Box b2 = b1;

In this code, both “b1” and “b2” refer to the same object. The assignment of “b1” to “b2” did not allocate any memory or copy any part of the original object. It simply makes “b2” refer to the same object as “b1”. Any changes made to the object through “b2” will affect the object to which “b1” is referring since they are the same object.

5. Parameters and Arguments

A parameter is a variable defined by a method that receives a value when the method is called. An argument is a value that is passed to a method when it is invoked.

For example, in the following method:

int square(int i){
return i * i;
}

“i” is a parameter. When the method is called, the value passed to it, such as “square(100)”, is the argument. Inside the method, the parameter “i” receives that value.

--

--

Nikita pandey

Python developer| Data science student| detective by nature| into art,AI, and books