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

Variables in Java

The document explains the different types of variables in Java, including local, instance, and static variables. Local variables are defined within methods and have a limited scope, instance variables are associated with class instances, and static variables belong to the class itself and are shared among all instances. It also details the initialization and access rules for each variable type, along with examples for better understanding.

Uploaded by

manichinniah94
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)
4 views

Variables in Java

The document explains the different types of variables in Java, including local, instance, and static variables. Local variables are defined within methods and have a limited scope, instance variables are associated with class instances, and static variables belong to the class itself and are shared among all instances. It also details the initialization and access rules for each variable type, along with examples for better understanding.

Uploaded by

manichinniah94
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/ 6

VARIABLES IN JAVA

LOCAL VARIABLES

1. public class StudentDetails {


public void StudentAge()
{

int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}

public static void main(String args[])


{
StudentDetails obj = new StudentDetails();
obj.StudentAge();
}
}

2. public class StudentDetails {


public void StudentAge()
{ // local variable age
int age = 0;
age = age + 5;
}

public static void main(String args[])


{
// using local variable age outside it's scope
System.out.println("Student age is : " + age);
}
}
INSTANCE VARIABLES

1. import java.io.*;
class Marks {

int engMarks;
int mathsMarks;
int phyMarks;
}

class MarksDemo {
public static void main(String args[])
{
// first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;

// second object
Marks obj2 = new Marks();
obj2.engMarks = 80;
obj2.mathsMarks = 60;
obj2.phyMarks = 85;

// displaying marks for first object


System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);

// displaying marks for second object


System.out.println("Marks for second object:");
System.out.println(obj2.engMarks);
System.out.println(obj2.mathsMarks);
System.out.println(obj2.phyMarks);
}
}
STATIC VARIABLES

1. import java.io.*;
class Emp {

// static variable salary


public static double salary;
public static String name = "Harsh";
}

public class EmpDemo {


public static void main(String args[])
{

// accessing static variable without object


Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:"
+ Emp.salary);
}
}

2.
class VariableDemo
{
static int count=0;
public void increment()
{
count++;
}
public static void main(String args[])
{
VariableDemo obj1=new VariableDemo();
VariableDemo obj2=new VariableDemo();
obj1.increment();
obj2.increment();
System.out.println("Obj1: count is="+obj1.count);
System.out.println("Obj2: count is="+obj2.count);
}
}
3. class JavaExample{
static int age;
static String name;
//This is a Static Method
static void disp(){
System.out.println("Age is: "+age);
System.out.println("Name is: "+name);
}
// This is also a static method
public static void main(String args[])
{
age = 30;
name = "Steve";
disp();
}
}

4. public class Demo{


public static void main(String args[]){
Student s1 = new Student();
s1.showData();
Student s2 = new Student();
s2.showData();
//Student.b++;
//s1.showData();
}
}

class Student {
int a; //initialized to zero
static int b; //initialized to zero only when class is loaded not for each object created.

Student(){
//Constructor incrementing static variable b
b++;
}

public void showData(){


System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
}
//public static void increment(){
//a++;
//}

Local Variables: A variable defined within a block or method or constructor is called


local variable.
 These variable are created when the block in entered or the function is called
and destroyed after exiting from the block or when the call returns from the
function.
 The scope of these variables exists only within the block in which the variable is
declared. i.e. we can access these variable only within that block.
 Initilisation of Local Variable is Mandatory.

Instance Variables: Instance variables are non-static variables and are declared in
a class outside any method, constructor or block.
 As instance variables are declared in a class, these variables are created when
an object of the class is created and destroyed when the object is destroyed.
 Unlike local variables, we may use access specifiers for instance variables. If
we do not specify any access specifier then the default access specifier will be
used.
 Initilisation of Instance Variable is not Mandatory. Its default value is 0
 Instance Variable can be accessed only by creating objects.

What is Static Variable in Java?


Static variable in Java is variable which belongs to the class and initialized
only once at the start of the execution. It is a variable which belongs to the class
and not to object(instance ). Static variables are initialized only once, at the
start of the execution. These variables will be initialized first, before the
initialization of any instance variables.

 A single copy to be shared by all instances of the class


 A static variable can be accessed directly by the class name and doesn’t
need any object

Static Variables: Static variables are also known as Class variables.


 These variables are declared similarly as instance variables, the difference is
that static variables are declared using the static keyword within a class outside
any method constructor or block.
 Unlike instance variables, we can only have one copy of a static variable per
class irrespective of how many objects we create.
 Static variables are created at the start of program execution and destroyed
automatically when execution ends.
 Initilisation of Static Variable is not Mandatory. Its default value is 0
 If we access the static variable like Instance variable (through an object), the
compiler will show the warning message and it won’t halt the program. The
compiler will replace the object name to class name automatically.
 If we access the static variable without the class name, Compiler will
automatically append the class name.
To access static variables, we need not create an object of that class, we can simply
access the variable as
class_name.variable_name;

Static variable initialization


1. Static variables are initialized when class is loaded.
2. Static variables are initialized before any object of that class is
created.
3. Static variables are initialized before any static method of the
class executes.

Default values for static and non-static variables are same.


primitive integers(long, short etc): 0
primitive floating points(float, double): 0.0
boolean: false
object references: null

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy