Free Jobs
Reference




Java Forums: Core Java (15) J2EE (3) EJB (5) Servlet (3) JDBC (6) Hibernate (2) Struts (1) Swing (1) Oracle (7) JSP (4) Other (10)

Topic: Types of Access Modifiers in Java and there use

To get the Posted Questions details daily in your mail Click Here

Share this Post on Facebook/Twitter/Orkut Bookmark and Share





User: manishav3388 Post Date: 01 Aug 2010 23:25

Introduction:
Access modifiers are used in java to define the level of visibility of variables ,methods and even classes etc .
Access modifiers are written before the type name or before the return type name in case of methods.



Main Content:
Following are the four types of access modifiers in java :

default
public
protected
private


Default:

It is visible to only classes in the same package.No keyword is there for declaring the default access modifier.
If no access modifier is used the default is applied.

For Example:

int noOfRecords;

char getUserName()
{

}

public:

It is visible to both inside and outside the class.public class members can be accessed by any other object.

For Example:

public int noOfRecords;

public char getUserName()
{

}

protected:

It is visible to methods in that class and subclasses of that class. This means that protected class members have visibility limited to subclasses.

For Example:

protected int noOfRecords;

protected char getUserName()
{

}

private:

It is visible only to the class in which they are defined. No other class can access to private class members.

Code example for private member variables:

For Example:

private int noOfRecords;

private char getUserName()
{

}

Conclusion:
Access modifiers are used in java to define the level of visibility of variables ,methods and even classes etc .
Access modifiers are written before the type name or before the return type name in case of methods.



About Writer:
hello,I am JAVA Developer from Noida.


Writer's Link: http://

Writer's Email:







Comments:
  • User: mohit555 Post Date:   08 Aug 2010 10:25
    This is very nice article.
    Thanks!

Post Comment





Similar Articles Top ArticlesLatest Articles Other Articles by this Author







www.freejobsreference.com Copyright © 2009-2011 | All Rights Reserved
All content of this website is protected by copyright laws and should not be reproduced in any form or by any means without written permission
(Best viewed in IE 8.0+ or Firefox 3.0+ at resolution of 1024 * 768 or higher)