What is difference between static and non-static variables

What is difference between static and non-static variables

Answer:

Static

Non-static

static method belongs to the classnon-static method belongs to an object of a class
Static member that will be used Independently of any object of that classA non-static a class member must be accessed only in conjunction with an object of its class
Static member can be used by itself, without reference to a specific instanceNon-static member can only be accessed by object of a class that it belongs to
When a member is declared static, it can be accessed before any objects of its class are createdOn the other hand when member is non-static it can be only accessed by object of class where it belongs to
Instance variables declared as static are, essentially, global variablesNon-static variable is not global variable it depends on object of same class

 

Leave a Reply

Your email address will not be published. Required fields are marked *