HTML Java

Java Modifier


Modifier

There is no such kind of terms in java like access specifier or access modifier there is only modifier.

Proof: I have create 1 class and we know that we can’t use private with class name then you can see

Example

private class A
{
public static void main(String args[]){}
}
Try it »

Output:

Similarly, like static



public protected default (is not keyword another name is (private package or friendly)) private

These concepts are used to explain the accessibility scope of the class interface data member and function, constructor static, final, abstract.

These are modifiers as well as keyword.

Rule: public=default=protected with in same package.


public: Class, Nested Class, DM, MF, Constructor when use public when you want to take these things out of the package then you should use public.

When use default when you don’t want to take these things out of the package then you should use default.


Private: Nested class, DM, MF, constructor you don’t want to need these things go to outside the class.


Protected: Nested class, DM, MF, constructor when we use protected with these things then it can go outside the package but only in the child class and it can only on the reference of child class only.


Example

Package1;
Class A{
Protected int x;
}

Package2;
Class B extends A{
B b = new B();
b.x;
}
Try it »