Nothing Special   »   [go: up one dir, main page]

Modifiers

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Modifiers

-------------
Note : there is no access specifier kind of thing in java.

The modifiers are the permission levels which are attach with the class , methods
or variables or other members of class which
shows there level of use in some other class .

-->modifiers are studied at 2 level in java


1. Class level
2. Variable and method level.

Modifiers at class level


-------------------------------
1. public
2.<default>
3.final
4.abstract
5.strictfp

1.public : This modifier is used with class . If a class is public then that class
can be access in same package as well as in
other package.

package p1;
public class A1
{
public void m1()
{
System.out.println("Hello");
}
}

package p1;
public class B1
{
public void m5()
{
A1 a =new A1();
a.m1();
}
}

package p2;
import p1.A1;

public class C1
{
public void m6()
{
A1 a=new A1();
a.m1();
}
}

2.<default>:
----------------
When we are not use any modifier with class then that class is consider as
default .If a class is default then that class can
be access within any class of same package but not in the class of other package.

package p1;
class A1
{
public void m1()
{
System.out.println("Hello");
}
}

package p1;
public class B1
{
public void m5()
{
A1 a =new A1();
a.m1();
}
}

package p2;
import p1.A1;

public class C1
{
public void m6()
{
A1 a=new A1();
a.m1();
}
}

compile time error: A1 is not public in p1; cannot be accessed from outside package
import p1.A1;

3.final modifier:
----------------------
If a class is final then that class can be access by using object but by
inheritance , either that class acan belong to same
or different package.

package p1;
final class A1
{
public void m1()
{
System.out.println("Hello");
}
}

package p1;
public class B1
{
public void m5()
{
A1 a =new A1();
a.m1();
}
}

package p1;

public class C1 extends A1


{
public void m6()
{
A1 a=new A1();
a.m1();
}
}

compile time error: cannot inherit from final A1


public class C1 extends A1

4.abstract modifier
-------------------------
This modifier is used with class and if class is abstract then that class can be
access by using inheritance not by object
creation (instantiation).

rest is same as previous discussion.

5.strictfp modifier
------------------------
This modifier is used with class and if class is strictfp then if any calculation
is perform in that class then that calculation
outcome is 100% platform independent.
This modifier follows IEEE 754 standard for floating point numbers.

Modifiers at method and variable level


--------------------------------------------------
1.public : This modifier is used with method and variables .If method and variable
are public then they can access within any
class of same package as well as in the class of any other package .

2.private: This modifer is used with method and variable only . If method or
variable is private then that member can be
access within same class but not in other class.

3.<default>: If a we are not using any modifier with method or variable then that
is consider default. If a member is default
then that member can be access within same package but cannot be access in
class of other package.

4.protected: This modifier is used with method and variable only . If a


method/variable is protected then that member can
be access anywhere in any class of same package but in class of other
package it can be access by using the object of
child class and that object must be store in the reference of child.

protected = default(in any class of same pkg) + private (in class of other
pkg)

package p1;
public class A1
{
protected void m1()
{
System.out.println("Hello");
}
}

package p1;
public class B1
{
public void m5()
{
A1 a =new A1();
a.m1();
}
}

package p2;
import p1.A1;

public class C1 extends A1


{
public void m6()
{
//A1 a=new A1();// error: m1() has protected access in A1
//A1 a=new C1();// error: m1() has protected access in A1
C1 a=new C1();
a.m1();
}
}

5.final modifier
--------------------
This modifier is used with method and variable .(as we discuss before) . If
member is final then that member's value
or definition is not change.

final modifier can be used with parameter as well as loacl variable and if
local variable is final and that variable is not
used within the method then there is no error generate and if that variable is
used in method without initialization then
C.E. would generate.

6.static modifier
----------------------
This modifier is used with variable and method and inner class . If a
member is static then memory will be allocate to
that member only once.
7.native modifier
----------------------
This modifier is used with method only . If a method is native then it means
that method definition is available in some
other programming lang and to inform to the java about that implementaion
native keyword is used .

public naitve void wait();

--> As we know that native method is already implemented in some other lang
hence semicolon must be used after method
prototype.
-->Disadvantage: Due to native keyword sometimes java has to compromise with its
platform independent nature .

8.synchronized modifier:
---------------------------------
This modifier is used with method and blocks only . If a method is synchronized
then that method can be access by one
controller(Thread) only at a time , so that data consistency will be maintain.

Advantage : Data will never be corrupt .


Disadvantage: Waiting time is increased .

9.transient modifier
-------------------------
This modifier is used with variable only , if a variable is transient then original
value of that variable not send over the
network but the default value of that variable will be send.

network
transient int i=10;--------------------------------------> int i=0;

10.volatile
--------------

You might also like