Instance Variables in Java

Instance variables, also known as member variables or fields, are variables defined within a class for which each instantiated object of the class has its own copy. They represent the properties or state of an object.

Key Characteristics

  • Unique to Each Instance: Each object of the class has its own copy of instance variables, meaning changes to the instance variables of one object do not affect the instance variables of another object.
  • Declared Without static Keyword: Instance variables are declared without the static keyword.
  • Object-Level Scope: They are accessed through object references.
  • Lifecycle: Instance variables are created when an object of the class is created using the new keyword, and they are destroyed when the object is destroyed.
  • Default Values: Instance variables have default values if not explicitly initialized. For example, numeric types default to 0, boolean defaults to false, and object references default to null.
  • Accessibility: Instance variables are accessible throughout the class, including all methods, constructors, and blocks. They are tied to a specific instance of the class and accessed using an object reference.
  • Storage: Instance variables are stored in the heap area of memory.
  • Initialization: Instance variables and instance blocks are recognized and initialized just before the execution of the respective class constructor.
  • Access Modifiers: Instance variables can be declared with access modifiers (private, protected, public, or default) to control their visibility and accessibility.
Code Example
Output
Brand: Toyota
Model: Camry
Year: 2020
Price: 24000.0

Brand: Honda
Model: Accord
Year: 2021
Price: 26000.0

Explanation:

  • Instance Variables: The Car class has four instance variables: brand, model, year, and price. These variables are declared private to encapsulate the data.
  • Constructor: The constructor public Car(String brand, String model, int year, double price) initializes the instance variables for a new Car object.
  • Getter Methods: Methods like getBrand, getModel, getYear, and getPrice are used to access the values of instance variables.
  • Setter Methods: Methods like setBrand, setModel, setYear, and setPrice are used to modify the values of instance variables.
Example of Default Values for Instance Variables
Output
j: 0

Instance Methods In Java

Instance methods are methods that belong to instances of a class. These methods can access instance variables and other instance methods directly.

Key Characteristics

  • Belong to Instances: Instance methods are tied to instances of a class and operate on those instances.
  • Access to Instance Variables: They can access instance variables and other instance methods directly.
  • Can Access Static Members: Instance methods can also access static variables and methods.
  • Declared Without static Keyword: Instance methods are declared without the static keyword.
  • Can Use this Keyword: Instance methods can use the this keyword to refer to the current instance of the class.
  • Scope: Object-level
Code Example
Output
Brand: Toyota
Model: Camry
Year: 2020
Price: 24000.0

Brand: Honda
Model: Accord
Year: 2021
Price: 26000.0