ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Modifier Type

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

  1. class fan{
  2. private int data=3;
  3. private void msg(){System.out.println("I m a Fan");}
  4. }
  5.  
  6. public class Air{
  7. public static void main(String args[]){
  8. fan obj=new fan();
  9. System.out.println(obj.data);//Compile Time Error
  10. obj.msg();//Compile Time Error
  11. } }

2.public:The data members or member methods declared public can be declared outside the visibility of the class.

  1. //save by Fan.java
  2.  
  3. package pack;
  4. public class Fan{
  5. public void msg(){System.out.println("I m a Fan");}
  6. }
  7. //save by Air.java
  8.  
  9. package mypack;
  10. import pack.*;
  11.  
  12. class Air{
  13. public static void main(String args[]){
  14. Fan obj = new Fan();
  15. obj.msg();
  16. }
  17. }

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

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!