[go: up one dir, main page]

0% found this document useful (0 votes)
29 views8 pages

Local Variable & Instance Variables

The document explains different types of variables in Java: local, instance, and static variables, detailing their scope, memory allocation, and access methods. It includes code examples to illustrate how these variables are declared and used within classes and methods. Additionally, it compares classes and objects, emphasizing the relationship between them and the memory they occupy.

Uploaded by

billingssam519
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Local Variable & Instance Variables

The document explains different types of variables in Java: local, instance, and static variables, detailing their scope, memory allocation, and access methods. It includes code examples to illustrate how these variables are declared and used within classes and methods. Additionally, it compares classes and objects, emphasizing the relationship between them and the memory they occupy.

Uploaded by

billingssam519
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

A) Local Variables

 declare the local variable in inside the method, blocks or constructor


 local variables stored in stack memory
 Scope of the variables only within the method not outside the method

void m1( ) {
int a = 10; //local varibales
System.out.println(a); //possible
}

void m2( ) {
System.out.println(a); //not possible
}
B) Instance Variables & methods --------->

 Instance variables declare the inside the class but outside of the method
 Scope of the varaibles : inside the class all methods & constructor & blocks
 Instance varaible memory allocated (when the object is created --- Object is destroyed )
 This is stored in heap memory
 Instance area we can directly access to instance variables
 Static area we can access by using the Object referance variables --- this is permission
1) instance area :-
void m1( ) //instance method
{
// body instance area
}

2) static area :-
Static void m1( ) //Static method
{
// body Static area
}
C ) Example in code format of instance variables :-

package com.HomePracticeByRatan;

public class Test {


// instance variables
int a = 100;
int b = 200;

// Static method
public static void main(String[] args) // main method ;
{
// this area is called static area
// this is different area
Test t = new Test();
System.out.println(t.a);
System.out.println(t.b);
t.m1();
}

// instance method
// this is same area for instance variables.
void m1() { // this area is called as instance area
System.out.println(a)System.out.println(b); }
D ) Static Variables -------
 The static variable which declare the inside the class and outside the method with
static modifire is called as Static variables
 Scope is within the class
 In static method memory allocated the when .class file unloading before the main
method calling
 This memory stored in Non- heap memory
 Static variable & methods are access using the class name both static & instance
area --this is permission

E ) Static Example in code format ----

public class Test {


//static variables
static int a = 1000;
static int b = 2000;

// Static method
public static void main(String[] args) // main method ;
{
// this area is called static area
// this is different area
System.out.println(Test.a);
System.out.println(Test.b);
Test t = new Test();
t.m1();

// instance method
// this is same area for instance variables.
void m1() { // this area is called as instance area
System.out.println(Test.a);
System.out.println(Test.b);

}
E) Static variable Accessing by 3 ways :-

public class Test {

static int a = 1000;

// Static method
public static void main(String[] args) // main method ;
{
System.out.println(Test.a); //1way by using class name

System.out.println(b); //2way by directly acesses

Test t = new Test(); //3 way by using Object


System.out.println(t.a);
}

Variables VS default value :-


Case 1 :- instance variable jvm will assign the default value

public class Test {

int a ; // default value is 0


boolean b ; // default value is false

public static void main(String[] args) // main method ;


{
Test t = new Test();
System.out.println(t.a);
System.out.println(t.b);
}
}
Case 2 :- In static variable jvm will assign the default
value

public class Test {

static double a ; // default value is 0.0


static boolean b ; // default value is false

public static void main(String[] args) // main method ;


{

System.out.println(Test.a);
System.out.println(Test.b);

}
}

Case 3 :- local variables

public class Test {

public static void main(String[] args) // main method ;


{ (defaulte value of local variables)
int a ; // The local variable a may not have been
initialized
int b ; // The local variable a may not have been
initialized

System.out.println(a);
System.out.println(b);

}
}
Class Vs Object

 Class is a logical entity codence logic / Object is a phyisical


entity it is representing memory
 Class is a blueprint it decide object creation without class it is
no Object
 Class is based on single blueprint there it is possible create
multiple object but evrey object occupied memory
 Declare the class using class keyword / create the object by
using new keyword
Summary of variables
Property Local Instance Static
Variables Variables Variables
Where declare Inside the method Inside the Class but Inside the Class but
outside of the Method outside of the Method
with Static modifire
Scope of the var. Within the method, Whithin the Class Whithin the Class
constructor,or blocks
When memory When method is start Instance always linked When .class file is
allocated & with object when loading
create object

When method is When ditroyed the When .class file is


When memory
completed object unloading
distroyed
Initial values Not assign We must provide We must provide
initialization initialization
How to access directly By using Object By using Class name
Relation with Object No relation with object Every object creation Satic memory create
memory is allocated any no. of object
Every object separate That allocate single
memory memory
Stored memory Stack meomry Heap memory Non- heap
memory
Assignment no. 1
public class Test {
// 2instance variables
int a = 10;
int b = 20;

static void m1()


// print 2 vaiables
{ Test t = new Test();
System.out.println(t.a);
System.out.println(t.b);

}
static void m2()
// print 2 variables
{ Test t = new Test();
System.out.println(t.a);
System.out.println(t.b);

}
public static void main(String[] args)
{ //call m1();
// Call m2();
Test.m1();
Test.m2();

}
}
Assignment no.2
public class Test {
// 2 instance variables
int a = 10;
int b = 20;

// 2 static variables
static int c = 30;
static int d = 40;

void m1() {
// print 4- variables
System.out.println(a);
System.out.println(b);
System.out.println(Test.c);
System.out.println(Test.d);
}
static void m2() {
// print 4- variables
Test t = new Test();
System.out.println(t.a);
System.out.println(t.b);
System.out.println(Test.c);
System.out.println(Test.d);

}
public static void main(String[] args) {
// call m1();
Test t = new Test();
t.m1();
// call m2();
Test.m2();

You might also like