Variables
Named memory location to store data
Example
int age = 20;
TYPES OF VARIABLES
1.Global Variables
2.Local Variables
Simple Memory Concept
Class Area
-----------
Static Variable (1 copy)
Object Area
-----------
Object1 → Instance variables
Object2 → Instance variables
Method Area
-----------
Local variables
Global Variables
Declared inside a class but outside a methods
used by all methods of the class.
class Only_Global
{
static String name;
static String breed;
static int cost;
public static void main(String args[])
{
name = "Scooby";
breed = "Pug";
cost = 1000;
System.out.println(name);
System.out.println(breed);
System.out.println(cost);
}
}
class GlobalExample
{
String name = "Scooby"; // global variable
int cost = 1000; // global variable
public static void main(String args[])
{
GlobalExample d = new GlobalExample();
System.out.println(d.name);
System.out.println(d.cost);
}
}
Types Of Global Variables
1. Instance Variables or NonStatic Variables
- Instance variables belong to an object
int id;
String name;
- Each object gets separate memory
Student s1 = new Student();
Student s2 = new Student();
- Memory Copy depends on object size
new Student()
- Object required to call Non-Static Method
s1.display();
s2.display();
Example
class Dog
{
String breed; // instance variable
public static void main(String args[])
{
Dog d1 = new Dog();
Dog d2 = new Dog();
d1.breed = "Pug";
d2.breed = "Labrador";
System.out.println(d1.breed);
System.out.println(d2.breed);
}
}
2.Static Variables
- Belong to the class
Belong to the class,not to individual objects
class Student {
static String college = "ABC College";
}
- Only one memory copy Only one memory copy is created,no matter how many objects are created.
class Test {
static int count = 0;
}
- All objects share the same value Only one copy, All objects share the same value
class Test {
static int x = 10;
}
public class Main {
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t1.x = 50;
System.out.println(t2.x);
}
}
Output
50
Explanation
Both objects share the same variable.
- Static Variables are declared inside the class Static Variables are declared inside the class,but outside the methods
class Example {
static int number = 100;
}
- Static Variables can be called without creating an object Called using Class name
class Demo {
static void show() {
System.out.println("Hello");
}
}
public class Main {
public static void main(String[] args) {
Demo.show();
}
}
- Static methods can access only static variables Cannot directly access Non-Static Variables
class Demo {
static int a = 10;
int b = 20;
static void display() {
System.out.println(a);
}
}
Example
class Dog
{
static String name;
public static void main(String args[])
{
Dog d1 = new Dog();
Dog d2 = new Dog();
d1.name = "Scooby";
System.out.println(d1.name);
System.out.println(d2.name);
}
}
Local Variables
- Declared inside a method
Declared inside a method, constructor, or block
class Test {
void show() {
int x = 10; // local variable
System.out.println(x);
}
public static void main(String[] args) {
Test t = new Test();
t.show();
}
}
Output
10
Explanation
Here x is declared inside the method show(), so it is a local variable.
- Used only inside that method(Limited Scope)
class Test {
void display() {
int num = 5; // local variable
System.out.println(num);
}
public static void main(String[] args) {
Test t = new Test();
t.display();
// System.out.println(num); // ERROR
}
}
Output
Error
Explanation
num works inside display(), but using it in main() causes an error.
- Short Lifetime (exists only while method runs)
class Test {
void count() {
int a = 20; // local variable
System.out.println(a);
}
public static void main(String[] args) {
Test t = new Test();
t.count();
t.count();
}
}
Explanation
Every time count() runs, the variable a is created again and destroyed when the method ends.
Output
20
20
- Must be initialised before use
class Test {
public static void main(String[] args) {
int x;
x = 10; // initialization
System.out.println(x);
}
}
Explanation
If we do not assign a value before using x, Java gives an error.
Output
10
- No default Value
class Test {
public static void main(String[] args) {
int a;
// System.out.println(a); // ERROR because not initialized
}
}
Example
class Only_Local
{
public static void main(String args[])
{
String name = "Scooby";
String breed = "Pug";
int cost = 1000;
System.out.println(name);
System.out.println(breed);
System.out.println(cost);
}
}
Global Variables (not Initialised = Assign Default Values)
class Dog
{
String name;
String breed;
int cost;
}
class Demo
{
public static void main(String[]args)
{
Dog d = new Dog();
System.out.println(d.name);
System.out.println(d.breed);
System.out.println(d.cost);
d.name = "Scobby";
d.breed = "Pug";
d.cost = 10000;
System.out.println(d.name);
System.out.println(d.breed);
System.out.println(d.cost);
}
}
Sharing Data Between Objects Using Static Variables
class Dog
{
static String name;
String breed;
int cost;
}
class Global_Error
{
public static void main(String args[])
{
Dog d1 = new Dog();
Dog d2 = new Dog();
d1.name = "scooby";
d1.breed = "pug";
d1.cost = 1000;
System.out.println(d1.name);
System.out.println(d2.name);
}
}
//Dog d1 = new Dog();
//Dog d2 = new Dog();
//d1.breed = "Pug";
//d2.breed = "Labrador";
//System.out.println(d1.breed);
//System.out.println(d2.breed);
Using Global Variables Inside Local Variables in Java
class global_inside_local
{
static String globalName = "Scooby"; // static global variable
int globalCost = 1000; // instance global variable
public static void main(String args[])
{
// Local variable using global static variable
String localName = globalName + " the Dog"; // using static global variable
System.out.println(localName);
// Create object to access instance variable
global_inside_local d = new global_inside_local();
// Local variable using instance global variable
int localCost = d.globalCost + 500; // correct
System.out.println(localCost);
}
}
Using Local Variables to Assign Values to Global Variables in Java
class local_inside_global
{
String name; // global variable
int cost; // global variable
public static void main(String args[])
{
Dog d = new Dog();
String localName = "Scooby"; // local variable
int localCost = 1000; // local variable
d.name = localName; // assigning local to global
d.cost = localCost;
System.out.println(d.name);
System.out.println(d.cost);
}
}
Top comments (0)