0% found this document useful (0 votes)
5 views

6.core Java Variables

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

6.core Java Variables

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Leela Soft Core Java (J2SE) Madhusudhan

Class Members:
class Test
{
1.Variables;
2.Methods;
3.Constructors;
4.instance blocks;
5.static blocks;
}

1. Variables:
Types of Variables:
1. Instance Variables
2. Static Variables
3. Local Variables
4. Parameters

Variable Declaration:
<data type> <identifier>;
Examples:
int x;
float y;
String z;

Initialization:
<identifier> = <value / expression>;
Examples:
x = 10;
y = 1.2f;
z = "Welcome to Java";
x = x+30*5;

Variable Declaration and Initialization:


<data type> <identifier> = <value/expression>;

Example:
int x = 10;
double y = 1.23;
boolean b = true;

Static Area:
Instance Area:

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

Instance Variables:
Example:
class Test {
int x = 10; // instance variable
static int y = 20; // static variable

void m1(int x1, double y1) // parameters


{
int z; // local variable
System.out.println("Method m1 :" + x);
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
System.out.println("Method main :" + t.x);

t.m1(100, 2.3);
}
}

Example:
class Test {
int x = 10; // instance variable
static int y = 20; // static variable

void m1(int x1, double y1) // parameters


{
int z; // local variable
System.out.println("Method m1 :" + x);
System.out.println("Method m1 :" + y);
System.out.println("Method m1 :" + Test.y);
}

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
System.out.println("Instance :" + t.x);

System.out.println("Static main :" + t.y);


System.out.println("Static main :" + Test.y);
System.out.println("Static main :" + y);

t.m1(100, 2.3);
}
}

Example:
class Test {
int x = 10; // instance variable
static int y = 20; // static variable

void m1(int x1, double y1) // parameters


{
int z = 30; // local variable
System.out.println("Method m1 :" + x);
System.out.println("Method m1 :" + y);
System.out.println("Method m1 :" + Test.y);
System.out.println("Method m1 :" + z);
System.out.println("Method m1 :" + x1);
System.out.println("Method m1 :" + y1);
}

public static void main(String[] args) {


int x = 100;
System.out.println("Main Method");
Test t = new Test();
System.out.println("Instance :" + t.x);

System.out.println("Static :" + t.y);


System.out.println("Static :" + Test.y);
System.out.println("Static :" + y);
System.out.println("Local :" + x);

t.m1(100, 2.3);
}
}

Example: Default Values


class Test {
int x; // instance variable

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

static int y; // static variable

void m1() // parameters


{
int z = 30; // local variable
System.out.println("Method m1 :" + x);
System.out.println("Method m1 :" + y);
System.out.println("Method m1 :" + Test.y);
System.out.println("Method m1 :" + z);
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
System.out.println("Instance :" + t.x);

System.out.println("Static :" + t.y);


System.out.println("Static :" + Test.y);
System.out.println("Static :" + y);

t.m1();
}
}

Example:
class Test {
public static void main(String[] args) {
int y;
System.out.println("Local :" + y);
}
}

error: variable y might not have been initialized

Memory areas in JVM:


1. Class area or Method area
2. Heap area
3. Stack area
4. PC Registers
5. Native Method Stacks

Instance Variable Vs Static Variables:


class Test {
int x; // instance variable
static int y; // static variable

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan

public static void main(String[] args) {


Test t1 = new Test();
t1.x = 10;
t1.y = 20;
System.out.println("Instance T1 :" + t1.x); // 10
System.out.println("Static T1 :" + t1.y); // 20

Test t2 = new Test();


t2.x = 100;
t2.y = 200;
System.out.println("Instance T2 :" + t2.x);
System.out.println("Static T2 :" + t2.y);

System.out.println("Instance :" + t1.x); // 10


System.out.println("Static :" + t1.y); // 20

Test t3 = new Test();


System.out.println(t3.x); // 0
System.out.println(t3.y); // 200

}
}

[email protected] Cell: 78 42 66 47 66

You might also like