Instance variable in Java

Java Programming Java8Object Oriented Programming

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.

raja
Published on 23-Apr-2018 06:59:11
Advertisements