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

Access Control, Usage of Static With Data and Methods, Usage of Final With Data, Nested Classes

Access control in Java determines how members of a class can be accessed. Public members can be accessed anywhere, private only within the class, and protected for inheritance. Static members can be accessed without an object and are shared among instances. Final prevents modification of variables. Nested classes exist within other classes and have access to outer members. Inner classes can access outer members but not vice versa.

Uploaded by

Ganesh Nelluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Access Control, Usage of Static With Data and Methods, Usage of Final With Data, Nested Classes

Access control in Java determines how members of a class can be accessed. Public members can be accessed anywhere, private only within the class, and protected for inheritance. Static members can be accessed without an object and are shared among instances. Final prevents modification of variables. Nested classes exist within other classes and have access to outer members. Inner classes can access outer members but not vice versa.

Uploaded by

Ganesh Nelluri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Access Control,

Usage of static with data and


methods,
Usage of final with data,
Nested classes
Access Control
• Encapsulation provides an important attribute:
access control.
• Through encapsulation, you can control what
parts of a program can access the members of
a class.
• By controlling access, you can prevent misuse.
• How a member can be accessed is determined by the
access specifier that modifies its declaration.
• Java’s access specifiers are
1. public
2. private
3. protected
• Java also defines a default access level.
• protected applies only when inheritance is involved.
public and private
• When a member of a class is specified as
public, then that member can be accessed by
any other code.
• When a member of a class is specified as
private, then that member can only be
accessed by other members of its class.
• Now you can understand why main( ) has
always been preceded by the public specifier.
• It is called by code that is outside the program
—that is, by the Java run-time system.
• When no access specifier is used, then by
default the member of a class is public within
its own package, but cannot be accessed
outside of its package.
• In the classes developed so far, all members of
a class have used the default access mode,
which is essentially public.
Access Control
• Usually, you will want to restrict access to the
data members of a class—allowing access only
through methods.
• At times, you will want to define methods that
are private to a class.
• An access specifier precedes the member’s
declaration statement.
LTC: Access Control
/* This program demonstrates the difference
between public and private. */
class Test {
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i)
{
c = i; // set c's value
}
int getc() {
return c; // get c's value
}
}
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
ob.a = 10;
ob.b = 20;
//ob.c = 100; // Error!
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}
}
• inside the Test class, variable a uses default
access, which for this example is the same as
specifying public.
• b is explicitly specified as public.
• Member c is given private access.
• This means that it cannot be accessed by code
outside of its class.
• So, inside the AccessTest class, c cannot be used
directly.
• It must be accessed through its public
methods: setc( ) and getc( ).
• If you were to remove the comment symbol
from the beginning of the following line,
// ob.c = 100; // Error!
then you would not be able to compile this
program because of the access violation.
Usage of static
with data and methods
Usage of static with data and methods

• Normally, a class member must be accessed


only in conjunction with an object of its class.
• To define a class member that will be used
independently of any object of that class,
precede its declaration with the keyword static.
• When a member is declared static, it can be
accessed before any objects of its class are
created, and without reference to any object.
• You can declare both methods and variables
to be static.
• The most common example of a static
member is main( ).
• main( ) is declared as static because it must be
called before any objects exist.
• Instance variables declared as static are,
essentially, global variables.
• When objects of its class are declared, no copy
of a static variable is made.
• Instead, all instances of the class share the
same static variable.
• Methods declared as static have several
restrictions:
– They can only call other static methods.
– They must only access static data.
– They cannot refer to this or super in any way. (The
keyword super relates to inheritance and is
described in the next chapter.)
• If you need to do computation in order to
initialize your static variables, you can declare
a static block that gets executed exactly once,
when the class is first loaded.
The following example shows a class that has
– a static method
– some static variables
– a static initialization block
LTC: Usage of static variables, methods, and blocks

class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
• Here is the output of the program:
Static block initialized.
x = 42
a=3
b = 12
Analysis of the program
• As soon as the UseStatic class is loaded, all of the
static statements are run.
• First, a is set to 3, then the static block executes,
which prints a message and then
initializes b to a * 4 or 12.
• Then main( ) is called,
which calls meth( ), passing 42 to x.
• The three println( ) statements print the two static
variables a and b, and the local variable x.
• Outside of the class in which they are defined,
static methods and variables can be used
independently of any object.
• To do so, specify the name of their class followed
by the dot operator.
• use the following general form:
classname.method( )
classname.variable

This is how Java implements a controlled version of


global methods and global variables
Usage of final with data

A variable can be declared as final. Doing so


prevents its contents from being modified.
For example:
final int FILE_NEW = 1;
final int FILE_OPEN = 2;
final int FILE_SAVE = 3;
• Subsequent parts of your program can now
use FILE_OPEN, etc., as if they were constants,
without fear that a value has been changed.
• It is a common coding convention to choose
all uppercase identifiers for final variables.
• Variables declared as final do not occupy
memory on a per-instance basis.
Nested Classes
• It is possible to define a class within another
class; such classes are known as Nested
classes.
• The scope of a nested class is bounded by the
scope of its enclosing class.
• Thus, if class B is defined within class A, then B
does not exist independently of A.
• A nested class that is declared within its
enclosing class is a member of its enclosing
class.
• It has access to all of the variables and
methods (including private members) of its
outer class and may refer to them directly.
• However, the enclosing class does not have
access to the members of the nested class.
inner class
• The most important type of nested class is the
inner class.
• The following program illustrates how to
define and use an inner class.
• The class named Outer has
– one instance variable named outer_x.
– one instance method named test( ).
– It defines one inner class called Inner.
inner class
class Outer {
int outer_x = 100;
void test() {
Inner in = new Inner();
in.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println(“outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer out = new Outer();
out.test();
}
}

Output from this application is shown here:


outer_x = 100
Analysis of the program
• In the program, an inner class named Inner is
defined within the scope of class Outer.
• Therefore, any code in class Inner can directly
access the variable outer_x.
• An instance method named display( ) is
defined inside Inner. This method displays
outer_x.
• The main( ) method of InnerClassDemo
creates an instance of class Outer and invokes
its test( ) method.
• That method creates an instance of class Inner
and calls the display( ) method.
• It is important to realize that an instance of
Inner can be created only within the scope of
class Outer.
• The Java compiler generates an error message
if any code outside of class Outer attempts to
instantiate class Inner.
• An inner class has access to all of the
members of its enclosing class, but the reverse
is not true.
• Members of the inner class are known only
within the scope of the inner class and may
not be used by the outer class.
• While nested classes are not applicable to all
situations, they are particularly helpful when
handling events (mouse click operations) to
simplify the code.
• Anonymous inner classes are inner classes that
don’t have a name.

End of session

You might also like