Lecture – 3.
JAVA variables and Modifiers
Contents
• Java variables
• Java Modifiers
2
Variables in java
3
Variables in java
Variables
Instance Class/Static
Local Variables
Variables Variables
4
1. Local variables
• Local variables are declared inside methods, constructors, or blocks.
• Local variables are visible only within the declared method, constructor or block.
• Access modifiers cannot be used for local variables.
5
2. Instance variables
• Instance variables are declared in a class, but outside a method, constructor or any block.
• Instance variables are created when an object is created with the use of the keyword 'new'
and destroyed when the object is destroyed.
• Access modifiers can be given for instance variables.
• The instance variables are visible for all methods, constructors and block in the class.
6
3. Class/static variables
• Class variables also known as static variables are declared with the static keyword in a class,
but outside a method, constructor or a block.
• Static variables are rarely used other than being declared as constants.
• Visibility is similar to instance variables.
• However, most static variables are declared public since they must be available for users of the
class.
7
Key points
❑ Local Variable
A variable that is declared inside the method
❑ Instance Variable
A variable that is declared inside the class but outside the method.
It is not declared as static.
❑ Static variable
Similar to instance variable but it is declared as static.
8
Example to understand the types of variables
Example - 1:
class A
{
int data=50; // instance variable
static int m=100; // static variable
void method()
{
int n=90; // local variable
}
}
9
Example - 2:
public class MyClass
{ public static void main(String[] args)
int a = 50; {
static int b = 100; MyClass ob = new MyClass ();
ob.method1 ();
public void method1() ob. method2 ();
{ }
int n = 90;
System.out.println(n); }
System.out.println(a);
System.out.println(b);
}
public void method2()
{
System.out.println(n);
System.out.println(a);
System.out.println(b);
}
10
Example - 3:
• public class NewClass {
• public static void sum()
• {
• int max = 100; //instance variable
• static int var = 50; // static variable • NewClass obj = new NewClass();
• System.out.println(obj.max);
• public static void main(String[] args) • System.out.println(var);
• { • //System.out.println(a+b);
• int a = 10, b = 20; // local variable • }
• System.out.println(a+b); • }
• NewClass obj = new NewClass();
• System.out.println(obj.max);
• System.out.println(var);
• sum();
• }
11
Modifiers in java
12
Modifiers
Modifiers are keywords that you add to those definitions to change their meanings.
There are two types of modifiers in java:
1. Access modifiers
2. Non-access modifiers
13
1. Access Modifiers
14
1. Access Modifiers in java
• The access modifiers in java specifies accessibility (scope) of a data
member, method, constructor or class.
• There are 4 types of java access modifiers:
1. private
2. default
3. protected
4. public
15
1) private access modifier
• The private access modifier is accessible only within the class.
Simple example of private access
modifier
In this example, we have created
two classes A and Simple. A class
contains private data member and
private method. We are accessing
these private members from outside
the class, so there is compile time
error.
16
private access modifier
17
2) default access modifier
• If you don't use any modifier, it is treated as default by default.
• The default modifier is accessible only within package.
18
Example of default access modifier
In this example, we have created two
packages pack and mypack. We are accessing
the A class from outside its package, since A
class is not public, so it cannot be accessed
from outside the package.
In the above example, the scope of class A and
its method msg() is default so it cannot be
accessed from outside the package.
19
default access
modifier
20
3) protected access modifier
• The protected access modifier is accessible within package and outside
the package but through inheritance only.
• The protected access modifier can be applied on the data member, method
and constructor. It can't be applied on the class.
21
• Example of protected access
modifier
• In this example, we have created the two
packages pack and mypack. The A class of
pack package is public, so can be accessed
from outside the package. But msg method
of this package is declared as protected, so
it can be accessed from outside the class
only through inheritance.
22
protected
access
modifier
23
4) public access modifier
• The public access modifier is accessible
everywhere.
• It has the widest scope among all other
modifiers.
• Example of public access modifier
24
public
access modifier
25
26
2. Non-Access Modifiers
27
2. Non-Access Modifiers
• static modifier
▪ for creating class methods and variables
• final modifier
▪ for finalizing the implementations of classes, methods, and variables.
• abstract modifiers
▪ for creating abstract classes and methods.
• synchronized and volatile modifiers
▪ which are used for threads.
28
Thank you!
29