What is the difference between instance variables and static variables other than usage of static keyword?
Answers
Answered by
2
Static(Class) variables and instance variables both are member variables s because they are both associated with a specific class, but the difference between them is Class variables only have one copy that is shared by all the different objects of a class, whereas every object has it's own personal copy of an instance
Chakshu290703:
Thnx
Answered by
7
Note: I have already answered this.https://brainly.in/question/3033505
Instance Variables:
(a) These are declared inside the class but outside of the method.
(b) The scope of the variable is inside the class all methods, blocks and constructors can able to access.
(c) Memory is allocated when an object is created and memory is destroyed when an object is destroyed.
(d) These are also known as class-level variables.
Ex: In the following example, a and b are instance variables because they are declared inside of the class but outside of the method.
class Demo
{
int a = 10;int b = 10;
public static void main(String args[])
{System.out.println(a);
System.out.println(b);
}}
Output: Compilation Error
Because I have already told that we need to create an object that is Instance variable memory is allocated when we create an object.
Ex: class Demo
{
int a = 10;
public static void main(String args[])
{
Demo devil = new Demo(); ----------------------- Object Creation.System.out.println(devil.a); ----------------------- Objectreference.classname}}
Output:10
Static Variables:
(a) These are declared inside the class but outside of the method with the static modifier.
(b) The cope of the variable within the class
(c) Memory is allocated when .class file loads
.
(d) Static variables are stored in the non-heap area.
(e) Can be accessed with the class name.Variable name.
Ex: In the below example a and b are called as Static variables.
Class Demo
{
static int a = 20;
public static void main(String[] args)
{
Demo devil = new Demo();
System.out.println(devil.a);
}}
Hope this help!
Instance Variables:
(a) These are declared inside the class but outside of the method.
(b) The scope of the variable is inside the class all methods, blocks and constructors can able to access.
(c) Memory is allocated when an object is created and memory is destroyed when an object is destroyed.
(d) These are also known as class-level variables.
Ex: In the following example, a and b are instance variables because they are declared inside of the class but outside of the method.
class Demo
{
int a = 10;int b = 10;
public static void main(String args[])
{System.out.println(a);
System.out.println(b);
}}
Output: Compilation Error
Because I have already told that we need to create an object that is Instance variable memory is allocated when we create an object.
Ex: class Demo
{
int a = 10;
public static void main(String args[])
{
Demo devil = new Demo(); ----------------------- Object Creation.System.out.println(devil.a); ----------------------- Objectreference.classname}}
Output:10
Static Variables:
(a) These are declared inside the class but outside of the method with the static modifier.
(b) The cope of the variable within the class
(c) Memory is allocated when .class file loads
.
(d) Static variables are stored in the non-heap area.
(e) Can be accessed with the class name.Variable name.
Ex: In the below example a and b are called as Static variables.
Class Demo
{
static int a = 20;
public static void main(String[] args)
{
Demo devil = new Demo();
System.out.println(devil.a);
}}
Hope this help!
Similar questions