Defining Instance Variables

  1. Instance variable are non-static variables
    **** The definition of an instance variable do NOT include the keyword static.

  2. The access speficier private states that the variable can NOT be accessed from a method outside the defining class.

    NOTE: methods defined inside the BankAccount can access the private variables.

    NOTE: it is a good practice to make all instance variables private to prevent other users from "messing up" the values of the instance variables.


    Example:
       public class myClass
        {
          private int Data1;
          public  int Data2;
    
          public void myMethod1(...)
           {
             ... This method can access Data1 & Data2
           }
        }
    
       public class myProgram
        {
          public static void main(String args[])
           {
    	 myClass x = new myClass();  // Create an "myClass" object
    
    	 Program CANNOT access x's Data1 variable !
    	 Program can access x's Data2 variable (using x.Data2)
    	 Program can invoke x's myMethod1 (using x.myMethod1(...))
           }
        }
    

  3. You have to specify a type for every variable.

    The type can be:

  4. The name of the variable is required.

What happens when you define a BankAccount object

NOTE: you can clearly see that: