Computer Science, asked by Anonymous, 1 year ago

Can anyone explain all the different types of variables used in JAVA...??

Class 10 ICSE question...

Plzz no spamming....

And thankx for your help.....

Answers

Answered by Niyathi738
6
1.Local Variables
•Local variables are declared in methods, constructors, or blocks. •Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
•Access modifiers cannot be used for local variables.

2.Instance Variables
•Instance variables are declared in a class, but outside a method, constructor or any block. •When a space is allocated for an object in the heap, a slot for each instance variable value is created.
•Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed.
•Instance variables can be declared in class

3.Local Variables
•Local variables are declared in methods, constructors, or blocks. •Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
•Access modifiers cannot be used for local variables.
•Local variables are visible only within the declared method, constructor, or block.
•Local variables are implemented at stack level internally.

Hope it helps u!!

Niyathi738: wlc :)
Dårviñ714: This are data types
Dårviñ714: Right.
Anonymous: ges
Anonymous: yes*
siddhartharao77: Correct ur answer. The question is related to variables
Anonymous: ^^
Niyathi738: Hey it is not coming in points...it is jumbled..pls adjust
Anonymous: thank u vry much
Niyathi738: wlc. :)
Answered by siddhartharao77
10
Here is a Brief Explanation of Variable types in JAVA: 

(1) Local variables:

(a) These are created or declared inside a block or constructor or method.

(b) The scope of the variable is within the method or constructor or blocks.

(c) Memory is allocated when the method starts and memory is released when the method ends.

(d) They are stored in stack memory.


Ex: In the below program a and b are called as local variables. 
 
Class Demo
      {
      public static void main(String[] args)
      {
      int a = 10;
      int b = 10;
     }
     }



(2) 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



(3) 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 helps!

siddhartharao77: This much knowledge is enough.
Rajdeep11111: Wow Bro!!!!!!
Rajdeep11111: Hats off to your knowledge!!
siddhartharao77: :-)
Anonymous: gr8 ans bro
Similar questions