‘Public’ Access specifier in JAVA


AutomationChinnu

Using access control you can control what parts of a program can access the members of a class (i.e. instance variable and methods).

By controlling access, you can prevent misuse. For example, allowing access to data only through a well defined set of methods, you can prevent misuse of that data.

However access control to instance variables and methods of a class is determined by access specifier.

Java’s access specifier’s are:

  • public
  • private
  • protected
‘public’ access specifier:
Example of an instance variable having ‘public’ access specifier:
public int a=5;
Example of a method having ‘public’ access specifier:
public void Add( )
{
     System.out.println(“Text inside Add( ) method”);
}
When a member of a class i.e. either instance variable or method is specified as ‘public’, then the member can be access by any other code outside the class.
Lets implement this on Eclipse IDE:  (Accessing ‘public’ instance variables…

View original post 588 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s