Variables that are shared by every instances of a class are ________?

2.7K    Asked by AnilJha in Java , Asked on Oct 11, 2022

 I'm very new to Java and want to understand the difference between class variables and instance variables.

For example:

class Bicycle { 
    static int cadence = 0; 
    int speed = 0; 
    int gear = 1; 
}

How are instance variables and class variables different from each other? Which variables here are class variables, and which are instance variables? How does this affect scope?

Answered by Andrew Jenkins

The answer to your question - variables that are shared by every instances of a class are ________ is - 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 its 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;
}
Full example:
// INSTANCE VARIABLE
public class Main {
    public static void main(String[] args) {
        Product prod1 = new Product();
        prod1.Barcode = 123456;
        Product prod2 = new Product();
        prod2.Barcode = 987654;
        System.out.println(prod1.Barcode);
        System.out.println(prod2.Barcode);
    }
}
public class Product {
    public int Barcode;
}
The output will be:
123456
987654
Now, change the instance variable to a class variable by making it static:
//CLASS VARIABLE
public class Main {
    public static void main(String[] args) {
        Product prod1 = new Product();
        prod1.setBarcode(123456);
        Product prod2 = new Product();
        prod2.setBarcode(987654);
        System.out.println(prod1.getBarcode());
        System.out.println(prod2.getBarcode());
    }
}
public class Product {
    public static int Barcode;
    public int getBarcode() {
        return Barcode;
    }
    public void setBarcode(int value){
        Barcode = value;
    } }I used non-static methods to get and set the value of Barcode to be able to call it from the object and not from the class. The output will be following:987654
987654

Your Answer

Answer (1)

Variables that are shared by every instance of a class are called class variables. These variables are defined within a class but outside any instance methods. Class variables are shared across all instances of the class, meaning that if one instance modifies the class variable, the change will be reflected in all other instances as well.

Class Variables in Python

Here is an example to illustrate class variables in Python:

  class MyClass:    # This is a class variable    class_variable = 0    def __init__(self, instance_variable):        # This is an instance variable        self.instance_variable = instance_variable# Create instances of MyClassobj1 = MyClass(1)obj2 = MyClass(2)# Access class variableprint(obj1.class_variable)  # Output: 0print(obj2.class_variable)  # Output: 0# Modify class variableMyClass.class_variable = 10# Access class variable after modificationprint(obj1.class_variable)  # Output: 10print(obj2.class_variable)  # Output: 10# Modify instance variableobj1.instance_variable = 5# Access instance variableprint(obj1.instance_variable)  # Output: 5print(obj2.instance_variable)  # Output: 2

Key Points

  • Definition: Class variables are defined within the class but outside any instance methods.
  • Shared Across Instances: All instances of the class share the same class variable. Changing the class variable affects all instances.
  • Access: Class variables can be accessed using the class name or through any instance of the class.
  • Scope: Class variables have a scope that spans the entire class, meaning they can be accessed and modified by any method within the class.

Class Variables in Other Languages

Java Example

In Java, class variables are declared with the static keyword:

  public class MyClass {    // This is a class variable    public static int classVariable = 0;    // Instance variable    public int instanceVariable;    public MyClass(int instanceVariable) {        this.instanceVariable = instanceVariable;    }    public static void main(String[] args) {        // Create instances of MyClass        MyClass obj1 = new MyClass(1);        MyClass obj2 = new MyClass(2);        // Access class variable        System.out.println(obj1.classVariable);  // Output: 0        System.out.println(obj2.classVariable);  // Output: 0        // Modify class variable        MyClass.classVariable = 10;        // Access class variable after modification        System.out.println(obj1.classVariable);  // Output: 10        System.out.println(obj2.classVariable);  // Output: 10        // Modify instance variable        obj1.instanceVariable = 5;        // Access instance variable        System.out.println(obj1.instanceVariable);  // Output: 5        System.out.println(obj2.instanceVariable);  // Output: 2    }}

Conclusion

Class variables are useful for data that should be shared among all instances of a class. They provide a way to store information that is common to all objects created from a class. By understanding how class variables work, you can effectively manage shared data within your classes.

4 Months

Interviews

Parent Categories