filmov
tv
JAVA : Difference between a Class and an Instance variable in Java?

Показать описание
JAVA : Difference between a Class and an Instance variable in Java?
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
JAVA : Difference between a Class and an Instance variable in Java?
In Java, a variable can be either a class variable (sometimes called a static variable) or an instance variable.
A class variable is shared by all instances of a class, while an instance variable is unique to each instance of a class.
Here's an example:
public class MyClass {
public static int classVar = 0;
public int instanceVar = 0;
public MyClass() {
classVar++;
instanceVar++;
}
}
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
In this example, classVar is a class variable that is shared by all instances of MyClass, while instanceVar is an instance variable that is unique to each instance.
The value of classVar is incremented each time a new instance of MyClass is created, while the value of instanceVar is always 1 for each instance.
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
JAVA : Difference between a Class and an Instance variable in Java?
In Java, a variable can be either a class variable (sometimes called a static variable) or an instance variable.
A class variable is shared by all instances of a class, while an instance variable is unique to each instance of a class.
Here's an example:
public class MyClass {
public static int classVar = 0;
public int instanceVar = 0;
public MyClass() {
classVar++;
instanceVar++;
}
}
MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();
In this example, classVar is a class variable that is shared by all instances of MyClass, while instanceVar is an instance variable that is unique to each instance.
The value of classVar is incremented each time a new instance of MyClass is created, while the value of instanceVar is always 1 for each instance.