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.
static
Keyword: Instance variables are declared without the static
keyword.new
keyword, and they are destroyed when the object is destroyed.private
, protected
, public
, or default) to control their visibility and accessibility.public class Car {
// Instance variables
private String brand;
private String model;
private int year;
private double price;
// Constructor to initialize instance variables
public Car(String brand, String model, int year, double price) {
this.brand = brand;
this.model = model;
this.year = year;
this.price = price;
}
// Getter methods to access instance variables
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
// Setter methods to modify instance variables
public void setBrand(String brand) {
this.brand = brand;
}
public void setModel(String model) {
this.model = model;
}
public void setYear(int year) {
this.year = year;
}
public void setPrice(double price) {
this.price = price;
}
// Method to display car details
public void displayDetails() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Price: " + price);
}
public static void main(String[] args) {
// Creating objects of the Car class
Car car1 = new Car("Toyota", "Camry", 2020, 24000);
Car car2 = new Car("Honda", "Accord", 2021, 26000);
// Displaying details of car1
car1.displayDetails();
System.out.println();
// Displaying details of car2
car2.displayDetails();
}
}
Brand: Toyota
Model: Camry
Year: 2020
Price: 24000.0
Brand: Honda
Model: Accord
Year: 2021
Price: 26000.0
Car
class has four instance variables: brand
, model
, year
, and price
. These variables are declared private
to encapsulate the data.public Car(String brand, String model, int year, double price)
initializes the instance variables for a new Car
object.getBrand
, getModel
, getYear
, and getPrice
are used to access the values of instance variables.setBrand
, setModel
, setYear
, and setPrice
are used to modify the values of instance variables.xxxxxxxxxx
public class Test {
int j;
public void m1() {
System.out.println("j: " + this.j); // Will print default value of 0
}
public static void main(String[] args) {
Test t = new Test();
t.m1();
}
}
j: 0
Instance methods are methods that belong to instances of a class. These methods can access instance variables and other instance methods directly.
static
Keyword: Instance methods are declared without the static
keyword.this
Keyword: Instance methods can use the this
keyword to refer to the current instance of the class.xxxxxxxxxx
public class Car {
// Instance variables
private String brand;
private String model;
private int year;
private double price;
// Constructor to initialize instance variables
public Car(String brand, String model, int year, double price) {
this.brand = brand;
this.model = model;
this.year = year;
this.price = price;
}
// Instance method to display car details
public void displayDetails() {
System.out.println("Brand: " + this.brand);
System.out.println("Model: " + this.model);
System.out.println("Year: " + this.year);
System.out.println("Price: " + this.price);
}
public static void main(String[] args) {
// Creating objects of the Car class
Car car1 = new Car("Toyota", "Camry", 2020, 24000);
Car car2 = new Car("Honda", "Accord", 2021, 26000);
// Calling instance methods
car1.displayDetails();
System.out.println("");
car2.displayDetails();
}
}
Brand: Toyota
Model: Camry
Year: 2020
Price: 24000.0
Brand: Honda
Model: Accord
Year: 2021
Price: 26000.0