Modifier Type

0
103

Modifier Type

Modifiers are basically keywords that are added to change the meaning of a definition.They can be broadly classified to two types:

1.Access control modifier: They basically control the visibility.They can be:
1.private: The data members or member methods declared private have their scope only within the class.

Example
[java] class fan{
private int data=3;
private void msg(){System.out.println(“I m a Fan”);}
}

public class Air{
public static void main(String args[]){
fan obj=new fan();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
} }

[/java]

2.public:The data members or member methods declared public can be declared outside the visibility of the class.
[java] //save by Fan.java

package pack;
public class Fan{
public void msg(){System.out.println(“I m a Fan”);}
}
//save by Air.java

package mypack;
import pack.*;

class Air{
public static void main(String args[]){
Fan obj = new Fan();
obj.msg();
}
}
[/java]

Output: I m a Fan

3.protected:It has the scope within the package and all its sub classes.
4.default(package):It has the scope within the same package.

2.Non Access modifiers:They provide special properties to variables and methods.
1.Final:A field declared final cannot be modified.We must initialize the final field at the time of declaration.
2.Static:These modifiers are used to create class variables and class methods that can be accessed without instance of a class.
3.Abstract:These modifiers are used to create abstract classes and methods.
4.Synchronized :They can be accessed by only one thread at a time.
5.Volatile:They are used in multithreading programming.

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.