Tuesday, 27 November 2018

Java - Instance Variables


Instance variables can be declared after class declaration.

Memory will be created for these variables when the object is created.

Each one object will have separate memory for instance variables.\

Instance variables are the state of an object.

Instance variables can be called from instance methods and not from static methods.
Consider following example
class A
{
int x,y;// instance variables
   void assignValues(int a,int b)
         {
              x=a;
              y=b;
         }
   public static void main(String args[])
         {
              A ob1=new A();
              A ob2=new A();
              A ob3=new A();
              ob1.assignValues(1,2);
              ob1.assignValues(5,200);
              ob1.assignValues(10,20);
         }
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.