Instance variable in Java



Instance Variables

Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

Example

Live Demo

public class Tester {
   private String message;
   public Tester(String message){
      this.message = message;
   }
   public void printMessage(){
      System.out.println(message);
   }
   public static void main(String args[]) {
      Tester tester = new Tester("Hello World");
      tester.printMessage();
   }
}

Here message is an instance variable.


Advertisements