Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

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

Asked by Anil Jha Oct 11, 2022 5.6K views 2 answers
Share

About this question

 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?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 10, 2024

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.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.