Difference between static and non-static variables

What is the difference between static and non-static variables?

Static Variables:
- A static variable is associated with the class has only one copy per class but not for each object. An instance of a class does not have static variables.

- Static variables can be accessed by static or instance methods

- Memory is allocated when the class is loaded in context area at run time.

Non-Static Variables:
- Non-static variables will have one copy each per object. Each instance of a class will have one copy of non-static variables.

- Instance variables can be accessed only by the instance methods.

- Instance variables are allocated at compile time.

What is the difference between static and non-static variables?

- A static variable is shared among all instances of a class.

- A non-static variable is specific to a single instance of that class.

Eg: A static variable can be shared by all users for the current running system. It’s a globally available value and can be used by all users.
What is inheritance? Explain it with an example
What is inheritance? - Through inheritance, classes can inherit commonly used state and behavior from their parent class...
What is an overloaded method? Provide an example to explain it
What is an overloaded method? - Overloaded methods are the methods which have the same name but different signatures...
Define overriding. Explain it with an example
Define overriding - Overriding is done in a child class for a method that is written in the parent class...
Post your comment