fbpx

OOPS Interview Questions

Question #1 :

Difference between class variables and Instance Variables?

https://softwareengineering.stackexchange.com/questions/293478/what-are-the-differences-between-class-variables-and-instance-variables-in-java

 

They both are member variables, meaning that both are associated with a class. Now of course, there are differences between the two:

Instance variables:

These variables belong to the instance of a class, thus an object. And every instance of that class (object) has it’s own copy of that variable. Changes made to the variable don’t reflect in other instances of that class.

public class Product {
    public int Barcode;
}

Class variables:

These are also known as static member variables and there’s only one copy of that variable that is shared with all instances of that class. If changes are made to that variable, all other instances will see the effect of the changes.

public class Product {
    public static int Barcode;
}

Leave a Reply

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