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

Answer Posted / ranganathkini

Members marked public are accessible to members of any class
irrespective of the package.

Members marked private are only accessible to other members
of the same class.

Members marked protected are only accessible to other
members of the same class, members of a subclass and members
of other classes in the same package.

Member marked static are shared by all instances of that
class and hence r not related to any specific instance of
that class. Methods marked static cannot access non-static
members or fields of that class. But non-static members can
access static fields or methods.

Is This Answer Correct ?    111 Yes 21 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is preparedstatement in java?

545


What is a key in java?

489


What is the difference between preparedstatement and statement in java?

536


How will you print number in reverse (descending) order in BST.

565


What is variable in java?

508






Can a class be private?

499


What is method in java ?

612


How many bits is a 64 bit byte?

521


What is use of set in java?

477


In which order the iterator iterates over collection?

564


Can we have two methods in a class with the same name?

569


When do we use synchronized blocks and advantages of using synchronized blocks?

648


What are Normalization Rules? Define Normalization?

536


Does string isempty check for null?

550


What is object cloning in Java?

621