Access Modifiers In Java}

Access Modifiers in java



In java there are two types of modifiers :

[1] Access modifiers

[2] Non access modifiers

Access modifiers specifies the accessibility or scope of a data member(non static global variable),method,constructor or class.

Access modifiers are of four types:





There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc. Here, we will learn access modifiers

The four access levels are ‘

-Visible to the package, the default. No modifiers are needed.

-Visible to the class only (private).

-Visible to the world (public).

-Visible to the package and all subclasses (protected).

Default Access Modifier – No Keyword

java course in bangalore

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.For default access modifiers we dont use any keyword

A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.


Variables and methods can be declared without any modifiers, as in the following examples ‘

//save by Demo

1. package pack;

2. class Demo

3. {

4. void message()

5. {

6. System.out.println(“Hello”);

7. }

8. }

java training center bangalore


1. package mypack;


2. import pack.*;

3. class Run

4. {

5. public static void main(String args[])

6. {

7 Demo . Obj = new Demo(); //Compile Time Error

7. obj.message(); //Compile Time Error

8. }

9. }

From above given example we can see that we can access the method message() of class Demo in Run because message() having default access modifiers.

Private Access Modifier – Private

Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.

Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

Best core java training in bangalore

Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.We can access outside the class using setters method

Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.


The following class uses private access control ‘

class Demo


1. private int data=90;

2. private void message()

3. {

4. System.out.println(“Hello java”);

5. }

6. }


8. public class Run

9. {

10. public static void main(String args[])

11. {

12. Demo obj=new Demo();

13. System.out.println(; //Compile Time Error

14. obj.message(); //Compile Time Error

15. }

16. }

In above given example we can see that data variable and message() method both are private .So they cant access outside the class.

Advance java training in bangalore

Public Access Modifier – Public

A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.


The following function uses public access control ‘

//save by


2. package pack;

3. public class Demo

4. {

5. public void message()

6. {

7. System.out.println(“Hello Java”);

8. }

9. }

java/j2ee classes bangalore

1. //save by Run .java


3. packagemypack;

4. importpack.*;


6. class Run

7. {

8. public static void main (String args[])

9. {

10. Demo.obj=new Demo();

11. obj.message();

12. }

13. }

Output: Hello Java

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

Protected Access Modifier – Protected

Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.

core and advanced java institute in marathahalli


//save by

1. package pack;

2. public class Demo

3. {

4. protected void message()

5. {

6. System.out.println(“Hello”);

7. }

8. }

1. //save by

2. package mypack;

3. import pack.*;


5. class Run extends Demo

6. {

7. public static void main(String args[])

8. {

9. Run.obj=new Run();

10. obj.message();

11. }

12. }

Output: Hello

This is the basic about the access modifiers . To learn more interesting topics on java join Infocampus Software training Institute for java courses.

Infocampus provides java course in bangalore with the live project. Attend 4 days free demo classes for java training. Training is given by experienced trainers.Syllabus is according to getting the job easily.Infocampus provides best core java training in bangalore with the 100% placement assistance.Learn Core & Advance Java in the easiest way.Contact at : 9738001024 or for more details on Advance java training visit at:

Article Source:}

Theme: Overlay by Kaira Extra Text
Cape Town, South Africa