Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views19 pages

Modifier

The document explains class modifiers in Java, detailing the types of modifiers applicable to top-level and inner classes, including public, default, final, abstract, and strictfp. It also distinguishes between access specifiers and modifiers, emphasizing that all are considered modifiers in Java. Additionally, it covers the implications of final variables, member visibility, and the rules for initialization of final variables.

Uploaded by

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

Modifier

The document explains class modifiers in Java, detailing the types of modifiers applicable to top-level and inner classes, including public, default, final, abstract, and strictfp. It also distinguishes between access specifiers and modifiers, emphasizing that all are considered modifiers in Java. Additionally, it covers the implications of final variables, member visibility, and the rules for initialization of final variables.

Uploaded by

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

Class Modifiers

Whenever we are writing our own classes compulsory, we have to provide some
information about our class to the JVM.
Like
1. Whether this class can be accessible from anywhere or not.
2. Whether child class creation is possible or not.
3. Whether object creation is possible or not etc.

We can specify this information by using the corresponding modifiers.


The only applicable modifiers for Top Level classes are:
1. public
2. default
3. final
4. abstract
5. strictfp
If we are using any other modifier, we will get compile time error.
Example:
private class Test
{
public static void main(String [] args)
{
int i = 0;
for(int j=0; j<3; j++)
{
i = i+j;
}
System.out.println(i);
}
}

Output:
Compile time error.
D:\Java>javac Test.java
Test.java:1: modifier private not allowed here
private class Test

But For the inner classes the following modifiers are allowed.

public

default private

final protected

abstract static

1
Q. What is the difference between access specifier and access modifier?

Ans. In old languages 'C' (or) 'C++' public, private, protected, default are considered as access
specifiers and all the remaining are considered as access modifiers.
But in java there is no such type of division all are considered as access modifiers.

Public Classes:

If a class declared as public then we can access that class from anywhere, within the
package or outside the package.

Example:
Program1:
package pack1;
public class Test
{
public void methodOne()
{
System.out.println("test class methodOne is executed");
}
}

Compile the above Program:


D:\Java>javac -d . Test.java

Program2:
package pack2;
import pack1.Test;
class Test1
{
public static void main(String [] args)
{
Test t = new Test();
t.methodOne();
}
}

Output:
D:\Java>javac -d . Test1.java
D:\Java>java pack2.Test1
Test class methodOne is executed.

If class Test is not public then while compiling Test1 class we will get compile time error
saying pack1.Test is not public in pack1; cannot be accessed from outside package.

2
Default Classes:

If a class declared as the default then we can access that class only within the current
package hence default access is also known as "package level access".

Example:
Program 1:
package pack1;
class Test
{
public void methodOne()
{
System.out.println("test class methodone is executed");
}
}
Program 2:
package pack1;
import pack1.Test;
class Test1
{
public static void main(String args[])
{
Test t = new Test();
t.methodOne();
}
}

Output:
D:\Java>javac -d . Test.java
D:\Java>javac -d . Test1.java
D:\Java>java pack1.Test1
Test class methodone is executed

3
final Modifier:
Final is the modifier applicable for classes, methods and variables.

final class: If a class declared as final, then we can’t create child class for that class i.e. we can’t
extend functionality of that class i.e. inheritance is not possible for final class.

Example:

final class P
{
}
class C extend P
{
}

Output:
Compile time error.
D:\Java>javac P.java
D:\Java>javac C.java
child.java:1: cannot inherit from final P
class C extends P

final Methods: If the Parent class method is declared as final then we can’t override that method
in the child class because its implementation is final.

Example:

class P
{
public void property()
{
System.out.println("cash+gold+land");
}
public final void marriage()
{
System.out.println("Tamanna");
}
}
class C extends P
{
public void marriage()
{
System.out.println("Priyanka");
}
}

4
Output:
Compile time error.
D:\Java>javac P.java
D:\Java>javac C.java
child.java:3: marriage() in C cannot override marriage() in P;
overridden method is final
public void marriage() {

final Variables: If a variable declared as final then we can’t perform reassignment of the
variable.

Example:

class Test
{
public static void main(String [] args)
{
final int i=10;
i=30;
}
}

Output:
Cannot assign a value to final variable.

5
Final variables:

Final instance variables:


➢ If the value of a variable is varied from object to object such type of variables are called
instance variables.
➢ For every object a separate copy of instance variables will be created.

➢ For the instance variables it is not required to perform initialization explicitly JVM will always
provide default values.

Example:

class Test
{
int i;
public static void main(String [] args)
{
Test t = new Test();
System.out.println(t.i);
}
}

Output:
D:\Java>javac Test.java
D:\Java>java Test
0

If the instance variable declared as the final compulsory, we should perform initialization
explicitly and JVM won't provide any default values. Whether we are using or not we will get
compile time error.

6
class Test
{
final int i;
}

Output:
Compile time error.
D:\Java>javac Test.java
Test.java:1: variable i might not have been initialized
class Test

Rule:
For the final instance variables, we should perform initialization before constructor
completion. That is the following are various possible places for this.

❖ At the time of declaration

Example:

class Test
{
final int i=10;
}

❖ Inside instance block

Example:

class Test
{
final int i;
{
i=10;
}
}

7
❖ Inside constructor

Example:

class Test
{
final int i;
Test()
{
i=10;
}
}

If we are performing initialization anywhere else, we will get compile time error.

class Test
{
final int i;
void m1()
{
i=10;
}
}

Output:
Compile time error.
D:\Java>javac Test.java
Test.java:5: cannot assign a value to final variable i
i=10;

8
Final static variables:
➢ If the value of a variable is not varied from object to object such type of variables is not
recommended to declare as the instance variables. We have to declare those variables at
class level by using static modifier.

➢ In the case of instance variables for every object a separate copy will be created but in the
case of static variables a single copy will be created at class level and shared by every
object of that class.

➢ For the static variables it is not required to perform initialization explicitly JVM will always
provide default values.

Example:
class Test
{
static int i;
public static void main(String [] args)
{
System.out.println("Value of i is:"+i);
}
}

Output:
D:\Java>javac Test.java
D:\Java>java Test
Value of i is: 0

If the static variable declares as final then compulsory, we should perform initialization
explicitly whether we are using or not otherwise we will get compile time error. (The JVM
won't provide any default values)

9
Rule:
For the final static variables, we should perform initialization before class loading
completion otherwise, we will get compile time error. That is the following are possible
places.

❖ At the time of declaration

Example:

class Test
{
final static int i=10;
}

❖ Inside static block

Example:

class Test
{
final static int i;
static
{
i=10;
}
}

If we are performing initialization anywhere else, we will get compile time error.

class Test
{
final static int i;
public static void main(String [] args)
{
i=10;
}
}

Output:
Compile time error.
D:\Java>javac Test.java
Test.java:5: cannot assign a value to final variable i
i=10;

10
Final local variables:
➢ To meet temporary requirement of the Programmer sometime we can declare the variable
inside a method or block or constructor such type of variables are called local variables.

➢ For the local variables JVM won't provide any default value compulsory we should perform
initialization explicitly before using that variable.

Example:
class Test
{
public static void main(String [] args)
{
int i;
System.out.println("Hello");
}
}

Output:
D:\Java>javac Test.java
D:\Java>java Test
Hello

Example:
class Test
{
public static void main(String [] args)
{
int i;
System.out.println(i);
}
}

Output:
D:\Java>javac Test.java
Test.java:5: variable i might not have been initialized
System.out.println(i);

11
• Even though local variable declared as the final before using only we should perform
initialization.

Example:
class Test
{
public static void main(String [] args)
{
final int i;
System.out.println("Hello");
}
}

Output:
D:\Java>javac Test.java
D:\Java>java Test
Hello

Note: The only applicable modifier for local variables is final if we are using any other
modifier, we will get compile time error.

Example:

class Test
{
public static void main(String [] args)
{
private int x=10; ➔Invalid
/* public int x=10; ➔Invalid
volatile int x=10; ➔Invalid
transient int x=10;*/ ➔Invalid
final int x=10; ➔Valid
}
}

Output:
D:\Java>javac Test.java
Test.java:5: illegal start of expression
private int x=10;

12
Formal parameters:
➢ The formal parameters of a method are simply acting as local variables of that method
hence it is possible to declare formal parameters as final.

➢ If we declare formal parameters as final then we can't change its value within the method.

Example:
class Test
{
public static void main(String [] args)
{
methodOne(10,20);
}
public static void methodOne(final int x, int y)
{
x=100; Formal parameters
y=200;
System.out.println(x+"...."+y);
}
}

Output:

D:\jp>javac Test.java
Test.java:10: error: final parameter x may not be assigned
x=100;
^

Conclusion:
✓ For instance, and static variables JVM will provide default values but if instance and
static declared as final JVM won't provide default value compulsory we should perform
initialization whether we are using or not.

✓ For the local variables JVM won't provide any default values we have to perform explicitly
before using that variables, this rule is same whether local variable final or not.

13
Member modifiers
Public members:

If a member declared as the public then we can access that member from anywhere "but
the corresponding class must be visible" hence before checking member visibility we have
to check class visibility.

Example:

package pack1;
class A
{
public void m1()
{
System.out.println("a class method");
}
}

D:\Java>javac -d . A.java

package pack2;
import pack1.A;
class B
{
public static void main(String args[])
{
A a = new A();
a.m1();
}
}

Output:
Compile time error.
D:\Java>javac -d . B.java
B.java:2: pack1.A is not public in pack1;
cannot be accessed from outside package
import pack1.A;

In the above Program even though m1() method is public we can't access from class B
because the corresponding class A is not public that is if both classes and methods are
public then only, we can access.

14
Default member:

If a member declared as default then we can access members only within the current
package i.e. from outside the package we can’t access. Hence, default access is known as
package level access.

Example 1:

package pack1;
class A
{
void m1()
{
System.out.println("m1 is executed");
}
}

package pack1;
import pack1.A;
class B
{
public static void main(String args[])
{
A a = new A();
a.m1();
}
}

Output:
D:\Java>javac -d . A.java
D:\Java>javac -d . B.java
D:\Java>java pack1.B
m1 is executed

15
Example 2:

package pack1;
class A
{
void m1()
{
System.out.println("a class method");
}
}
package pack2;
import pack1.A;
class B
{
public static void main(String args[])
{
A a = new A();
a.m1();
}
}

Output:
Compile time error.
D:\Java>javac -d . A.java 0
D:\Java>javac -d . B.java
B.java:2: pack1.A is not public in pack1; cannot be accessed from outside package
import pack1.A;

Private members:

If a member declared as the private then we can access that member only with in the current
class.

16
Protected members:

If a member declared as the protected then we can access that member within the current
package anywhere but outside package only in child classes.

We can access protected members within the current package anywhere either by child
reference or by parent reference

But from outside package we can access protected members only in child classes and
should be by child reference only that is we can't use parent reference to call protected
members from outside package.

Example:
Program 1:
package pack1;
public class A
{
protected void m1()
{
System.out.println("m1 is executed");
}
}

Prograam2:
package pack1;
class B extends A
{
public static void main(String args[])
{
A a = new A();
a.m1();
B b = new B();
b.m1();
A a1 = new B();
a1.m1();
}
}

Output:
D:\Java>javac -d . A.java
D:\Java>javac -d . B.java
D:\Java>java pack1.B
m1 is executed
m1 is executed
m1 is executed

17
Example 2:
package pack2;
import pack1.A;
class C extends A
{
public static void main(String args[])
{
A a = new A();
a.m1(); ✕
C c = new C();
c.m1(); ✓
A a1 = new C();
a.m1(); ✕
}
}

Output:
D:\Java>javac -d . C.java
C.java:8: error: m1() has protected access in A
a.m1();
^
C.java:12: error: m1() has protected access in A
a.m1();
^

18
Compression of private, default, protected and public:
Visibility private default protected public

Within the ✓ ✓ ✓ ✓
same class

From child × ✓ ✓ ✓
class of Same
package
From Non- × ✓ ✓ ✓
child Class of
Same Package
From child × × ✓(We should ✓
class of use child
Outside reference only)
package
From non- × × × ✓
child class of
outside
package

➢ The least accessible modifier is private.


➢ The most accessible modifier is public.
✓ private<default<protected<public

Note:
The modifiers which are applicable for inner classes but not for outer classes are private, protected,
static.
The modifiers which are applicable only for methods native.
The modifiers which are applicable only for variables transient and volatile.
The modifiers which are applicable for constructor public, private, protected, default.
The only applicable modifier for local variables is final.
The modifiers which are applicable for classes but not for enums are final , abstract.
The modifiers which are applicable for classes but not for interface are final.

19

You might also like