Instance Variable in Java

Let’s Start with Something Simple

Vehicle.java file in folder path: K:\Instance Variable

import model.Bike;  import java.util.List;  import java.util.ArrayList;    public class Vehicle {  	public static void main( String[] args ){    		List<Bike> bikes = new ArrayList<>();  		Bike bike1 = new Bike();  		bikes.add(bike1);  		Bike bike2 = new Bike();  		bikes.add(bike2);  		Bike bike3 = new Bike();  		bikes.add(bike3);    		for(Bike v : bikes) {  			System.out.println(v.name);  		}    	}  }

Bike.java file in folder path: K:\Instance Variable\model

package model;    public class Bike {    	public String name = "Honda";  	public long color = 0x2E0456;  	public int gearOrNot = 1;    }

Execution with errors:

  • javac Vehicle.java
    • Vehicle.java:1: error: cannot access Bike
    • import model.Bike;
    • ^
    • bad class file: .\model\Bike.class
    • class file contains wrong class: Bike
    • Please remove or make sure it appears in the correct subdirectory of the classpath.
  • K:\Instance Variable>cd model
  • K:\Instance Variable\model>javac Bike.java
  • K:\Instance Variable\model>cd ..
  • K:\Instance Variable>javac Vehicle.java
    • Vehicle.java:16: error: incompatible types: Bike cannot be converted to Vehicle                                                         for(Vehicle v : bikes) {                                                                                                                ^
    • Vehicle.java:17: error: cannot find symbol                                                                                                      System.out.println(v.name);                                                                                                                 ^
    • symbol:   variable name
    • location: variable v of type Vehicle
    • 2 errors
  • K:\Instance Variable>javac Vehicle.java
  • K:\Instance Variable>java Vehicle
  • Honda
  • Honda
  • Honda

Notes

  • We need to compile Bike.java first to prepare Bike.class
  • Writing ‘package model;’ is a must as it shows the relative package path.
  • Bike instance has the instance variable ‘name’ and not instance of Vehicle

Let’s increase the complexity

Vehicle.java file in folder path: K:\Instance Variable

import model.Bike;  import java.util.List;  import java.util.ArrayList;    public class Vehicle {  	public static void main( String[] args ){    		List<Bike> bikes = new ArrayList<>();  		Bike bike1 = new Bike();  		bikes.add(bike1);  		Bike bike2 = new Bike();  		bikes.add(bike2);  		Bike bike3 = new Bike();  		bikes.add(bike3);    		VehicleTypes vt = new VehicleTypes();    		int noOfWheels = vt.getWheels(bikes);  		System.out.println("Number of wheels: " +noOfWheels);  	}  }

 VehicleTypes.java file in folder path: K:\Instance Variable

//FOr demonstrating instance method  import java.util.List;  import model.Bike;    public class VehicleTypes {  	public int getWheels(List<Bike> bikes) {    		for(Bike v : bikes) {  			System.out.println(v.name);  		}    		return 2;  	}  }

Output

  • K:\Instance Variable>javac VehicleTypes.java
  • K:\Instance Variable>javac Vehicle.java
  • K:\Instance Variable>java Vehicle
  • Honda
  • Honda
  • Honda
  • Number of wheels: 2

Notes

  • VehicleTypes now has the method to deal with bikes object.
  • We need to import bike in VehicleTypes.
  • We create instance of VehicleTypes which helps us deal with bikes object. This reduces code in static void main method.
  • 3 things are to be noted:
    • Bike.java has the data
    • VehicleTypes has the logic to process the data
    • Vehicle.java creates the data and passes it to the VehicleTypes instance for processing.

Do It Yourself

  • Bike.java has data set to public which breaks the rules of encapsulation.
  • Make the data of Bike.java private and use Getters and Setters as the only means to access the data.