Class, Object, Variables Constructor
Class, Object, Variables Constructor
objects. It defines a set of properties (also known as fields or attributes) and methods (functions or behaviors)
that the objects created from the class will have.
In Java and other object-oriented programming languages, an object is an instance of a class. It represents a specific
entity that contains both data (in the form of fields or attributes) and behavior (in the form of methods).
Class Definition:
void displayInfo() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
Summary
Class: The Car class defines what attributes and behaviors a car object will have.
Object: car1 and car2 are specific instances of the Car class, each with its own make, model, and year.
These objects use the class's blueprint but hold their own unique data.
Instance Variables
Instance variables are variables defined within a class for which each instantiated object of the class has its
own separate copy. These variables represent the attributes or properties of an object and are used to store the
object's state. Instance variables are declared within a class but outside any method, constructor, or block.
Instance variables are accessed through objects. You can access and modify an instance variable using the
object reference followed by the dot operator (.) and the variable name.
Imagine a Car class with instance variables make, model, and year. When you create an object of this class, the
instance variables are stored in memory, and you access them via the object's reference.
Class: Car
-------------------------------------
| make | model | year |
-------------------------------------
Objects:
myCar1: myCar2:
--------------------- -------------------
| make = "Toyota" | | make = "Honda" |
| model = "Camry" | | model = "Civic" |
| year = 2021 | | year = 2020 |
-------------------- -------------------
// Constructor
public Car(String make, String model, int year) {
this.make = make;
this.model = model;
this.year = year;
}
5. Code Explanation
Instance Variables: The Car class has three instance variables: make, model, and year. Each object
created from the Car class will have its own unique values for these variables.
Constructor: The constructor Car(String make, String model, int year) initializes the instance
variables for each object. When an object is created, the values passed to the constructor are assigned to
the instance variables.
Method displayInfo: This method prints out the values of the instance variables. Since instance
variables are tied to the specific object that calls the method, it will print the details of the object that
invokes it.
Creating Objects: The main method creates two objects of the Car class (myCar1 and myCar2) with
different values for make, model, and year.
Accessing Instance Variables: The instance variables are accessed directly using the object references
myCar1 and myCar2. For example, myCar1.make returns "Toyota", and myCar2.make returns "Honda".
Output:
o When the program is run, it first prints the make of both cars directly.
o Then it calls the displayInfo() method on both myCar1 and myCar2, displaying the details of
each car.
Static Variables
In Java, static variables (also known as class variables) are variables that belong to the class rather than to
instances (objects) of the class. A static variable is shared among all instances of the class, meaning that if one
object modifies the static variable, the change is reflected across all objects of that class.
Within the Class: You can access a static variable directly by its name.
Outside the Class: You can access a static variable using the class name, like
ClassName.variableName.
Here’s a simple conceptual diagram to illustrate the difference between instance variables (object-specific) and
static variables (shared across all instances):
+------------------+
| Class |
+------------------+
| static int count |
+------------------+
|
| Shared among all instances
+-------+-------+-------+
| | | |
+-----v-----+ +-----v-----+ +-----v-----+
| Object1 | | Object2 | | Object3 |
+-----------+ +-----------+ +-----------+
| int value | | int value | | int value |
+-----------+ +-----------+ +-----------+
In the diagram:
Counter() {
count++; // Increment the static variable for each new object
}
void displayCount() {
System.out.println("Count: " + count);
}
}
public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
5. Code Explanation
Static Variable Declaration: The count variable is declared as static in the Counter class, making it a
class-level variable shared by all instances of the class.
Constructor: Each time a Counter object is created, the count variable is incremented. This means that
no matter how many objects are created, count will keep track of the total number.
Accessing Static Variables:
o c1.displayCount(); demonstrates how you can access the static variable using an object.
o Counter.count; shows how you can access the static variable directly using the class name.
In this example, the count variable will be incremented three times because three Counter objects (c1, c2, and
c3) are created. The output will be:
Count: 3
3
This output shows that count holds the same value across all objects, confirming that it is indeed a shared
(static) variable.
This table summarizes the key differences between instance and static variables in Java.
A constructor
1. Definition with Example
A constructor is a special method in a class that is invoked when an object of the class is created. It has the
same name as the class and no return type. Its primary purpose is to initialize the new object.
Example:
public class Car {
String model;
int year;
// Constructor
public Car(String model, int year) {
this.model = model; // Initialize instance variables
this.year = year;
}
void displayInfo() {
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
2. Application
3. Default Constructor
A default constructor is a no-argument constructor automatically provided by Java if no other constructors are
defined. It initializes instance variables to their default values.
Theoretical Points:
If other constructors are defined, you need to explicitly define a default constructor if you want it.
Example:
public class Car {
String model;
int year;
// Default constructor
public Car() {
// Default initialization
model = "Unknown";
year = 0;
}
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
}
No-Argument Constructor
// No-argument constructor
public Car() {
model = "Unknown";
}
}
Parameterized Constructor
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
}
6. Use of this Keyword with Proper Examples
// Parameterized constructor
public Car(String model) {
this.model = model; // 'this.model' refers to the instance variable
}
}
Example 2: Calling Another Constructor (Constructor Chaining)
public class Car {
String model;
int year;
// No-argument constructor
public Car() {
this("Unknown", 0); // Calls parameterized constructor
}
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
}
Constructor chaining is when one constructor calls another constructor in the same class.
Example:
public class Car {
String model;
int year;
// No-argument constructor
public Car() {
this("Unknown", 0); // Calls the two-argument constructor
}
// Single-argument constructor
public Car(String model) {
this(model, 0); // Calls the two-argument constructor with a default year
}
// Two-argument constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
}
8. Copy Constructor (Application and Example Code)
A copy constructor creates a new object as a copy of an existing object. It is useful for copying objects with
complex states.
Application:
Copying Objects: Useful when you need to create a duplicate of an object with the same state.
Clone Operations: Can be used in custom cloning implementations.
Example Code:
public class Car {
String model;
int year;
// Parameterized constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
}
// Copy constructor
public Car(Car other) {
this.model = other.model;
this.year = other.year;
}
void displayInfo() {
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
originalCar.displayInfo();
copiedCar.displayInfo();
}
}
In this example, copiedCar is created as a copy of originalCar using the copy constructor. Both cars will
have the same model and year values.