Access Control and Static Keyword
Lecture 5
Prof. Anita Agrawal
27-01-25
In today’s session…
Garbage collection
Access Specifiers
Static keyword
2 Anita Agrawal CS F213 1/29/2025 9:02 AM
Garbage Collection
In Java, objects are dynamically operated by
the new operator
How are objects destroyed and memory is
released for future reallocation?
In some languages, dynamically allocated objects
are manually released
Java handles deallocation automatically using
Garbage Collection
3 Anita Agrawal CS F213 1/29/2025 9:02 AM
When no reference to an object exists
Java assumes that the object is no longer needed
The memory occupied by object can be
reclaimed
No explicit need to destroy objects
Garbage collection occurs sporadically during
program execution.
[Link]();
4 Anita Agrawal CS F213 1/29/2025 9:02 AM
Access Specifiers ……revisited
5 Anita Agrawal CS F213 1/29/2025 9:02 AM
Controlling access to members of class
Access level modifiers determine whether other
classes can use a particular field or invoke a particular
method.
Two levels of access control:
At the top level—public, or package-private (no
explicit modifier).
At the member level—public, private, protected, or
package-private (no explicit modifier).
6 Anita Agrawal CS F213 1/29/2025 9:02 AM
Class modifiers:
Public: The class is visible to all classes
everywhere
Package-Private: (default): visible only
within its own package
7 Anita Agrawal CS F213 1/29/2025 9:02 AM
Class Member modifiers:
Public: The member is visible everywhere
Package Private: The member is visible only
within the package in which it has been
defined
8 Anita Agrawal CS F213 1/29/2025 9:02 AM
Protected: The member can only be accessed
within its own package (as with package-
private) and, in addition, by a subclass of its
class in another package
Private: The member can only be accessed in
its own class.
9 Anita Agrawal CS F213 1/29/2025 9:02 AM
Summary of the access members
Table illustrating the access control modifiers
10 Anita Agrawal CS F213 1/29/2025 9:02 AM
Access levels, why???
Access levels affect you in two ways.
First, when you use classes that come from another
source, such as the classes in the Java platform,
access levels determine which members of those
classes your own classes can use.
Second, when you write a class, you need to decide
what access level every member variable and every
method in your class should have.
11 Anita Agrawal CS F213 1/29/2025 9:02 AM
Tips on choosing access level
If other programmers use your class, you want to ensure
that errors from misuse should not happen. Access levels
can help you do this.
Use the most restrictive access level that makes sense
for a particular member. Use private unless you have a
good reason not to.
Avoid public fields except for constants. Public fields
tend to link you to a particular implementation and
limit your flexibility in changing your code.
12 Anita Agrawal CS F213 1/29/2025 9:02 AM
Encapsulation
➢ Access Control is an important aspect of
Encapsulation
➢ It is the mechanism that binds together code and
the data it manipulates
13 Anita Agrawal CS F213 1/29/2025 9:02 AM
Static modifier
When a number of objects are created from the
same class, they each have their own distinct copies
of instance variables.
Each object will have its own values for these
variables, stored in different memory locations.
14 Anita Agrawal CS F213 1/29/2025 9:02 AM
Sometimes, we want to have variables that are
common to all objects.
Use static modifier
Static variables are associated with the class, rather
than with any object.
Every instance of the class shares the class variable,
which is in one fixed location in memory.
15 Anita Agrawal CS F213 1/29/2025 9:02 AM
When a member is declared static, it can be accessed
before any objects of its class are created and without
reference to any object
16 Anita Agrawal CS F213 1/29/2025 9:02 AM
Static keyword
The static keyword is used mainly for memory
management.
It is applicable to
blocks
variables
nested classes
methods
17 Anita Agrawal CS F213 1/29/2025 9:02 AM
Static Keyword-Variables
It precedes variable declaration with the keyword static.
Ways of initialization:
Directly: modifier(if any) static var_type var_name;
Example: public static int x =2;
or……….
Declare a static block
Example: static int x;
static {
x = 2;
}
[Link]
18 Anita Agrawal CS F213
Observations for static
Any object can change the value of the class variable, but
class variables can also be manipulated without creating
an instance of the class.
Static block executes exactly once.
19 Anita Agrawal CS F213 1/29/2025 9:02 AM
Static Methods
Methods can also be declared as static.
Restrictions
They can access only static data of their class
They can directly call only other static methods of
their class
They can’t refer to this or super(Inheritance)
We can call a non-static method to display a
static variable.
20 Anita Agrawal CS F213 1/29/2025 9:02 AM
We can call a non-static method to display a static
variable
We cannot call a static method to display a non-static
variable
21 Anita Agrawal CS F213 1/29/2025 9:02 AM
Any Questions?
22 Anita Agrawal CS F213 1/29/2025 9:02 AM