Aryan PrajapatKnowledge Contributor
Explain static variables with examples and a diagram.
Explain static variables with examples and a diagram.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
A static variable in Java is a variable that is associated with the class rather than an instance of the class. This means that all instances of the class share the same static variable. They are declared using the static keyword.
public class Example {
static int count = 0;
public Example() {
count++;
}
}
Diagram – The diagram below illustrates the relationship between the class and the static variable:
+———-+
Example | count=0 |
+———-+
| |
Instance1 | |
+———-+
| |
Instance2 | |
+———-+