Home > Software > Java > Core-Java
Interview Questions   Tutorials   Discussions   Programs   

Core-Java - Explain the differences between public, private, protected and static?

asked marvit September 18, 2014 02:12 AM  

Explain the differences between public, private, protected and static?


1 Answers

answered By Madhumathi   0  

public access modifier : Can be accessed from any other class .

classes , methods , interfaces , variables can be declared using the public access modifier

private access modifier :

Can only be accessed by the declared class itself. Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

Variables that are declared private can be accessed outside the class if public getter methods are present in the class.

protected access modifier: 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.

The classes and interfaces cannot be declared as protected access modifier. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

static access modifier :

The static modifier can be used by methods and variables of the class.

The static key word is used to create variables that will exist independently of any instances created for the class. Only one copy of the static variable exists regardless of the number of instances of the class.

Static variables are also known as class variables. Local variables cannot be declared static.

   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!