Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

Access Modifiers In Java

A Java access modifier is used to implement encapsulation. Access modifiers can be specified separately for a class, its constructors, fields and methods. Classes, fields, constructors and methods can have one of four different Java access modifiers

 Private
 Default
 Protected
 Public

Private Access Modifier

If a method or variable is marked as private then it can be accessed with in the class only. private variable and method are not inherited.
Inner class also can be marked as private since it;s acting as data member. top level class can not marked as private.
private-access-specifier

In above Example the member variable "name" and method "foo()" has been marked as private. That means, that the member variable "name" and method "foo()" inside the Agiler class cannot be accessed from code outside the Agiler class.

default Access Modifier

if we do not mentioned any access specifier then by default access modifier is "default access modifier". the default access modifier is also sometimes referred to as the package access modifier.
default-access-specifier

Output :

coddybug
Hey! this is foo...
name is = coddybug
Hey! this is foo...

protected Access Modifier

If members are declared as protected then these are accessible to all classes in the package and to all subclasses of its class in any package where this class is visible.
protected-access-specifier


Public Access Modifier

If members are declared as public inside a class then such members are accessible through out the application(even out side the package). This is the least restrictive of all the accessibility modifiers.


public-access-specifier