Sunday 11 May 2014

What You will Learn :

  • What are Static/Class variables
  • What are Instance Variables
  • Scope of Static and Instance variables
  • Difference b/w Static and Class variables


Static Variables :

  • Static variables are associated with all the objects of the class
  • Declared in the class outside of any method or block
  • They are assigned default values when created i.e 0 for integers
  • They have the same state for all objects of the class
  • No object is needed to access them i.e we access them like this  "ClassName.VariableName"
  • They can be used in the static methods
  • Just one copy of static variables per class


This image will clear all the confusion :



Instance Variables :

  • As the name tells Instance Variables are associated with the Instances or Objects of the class
  • Declared in the class outside of any method or block
  • They are assigned default values when created i.e 0 for integers
  • Every object of the class have its own set of Instance Variables.
  • They may have different or same values(states) for all the objects
  • They require the object to be accessed outside of the class as "ObjectName.VariableName"
  • One copy of Instance Variables per Object



Here is an image for further explanation :



I hope you learned something from this post if you have any questions feel free to comment them below. Like us for more free stuff!!!

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!