---
title: "Access specifier and modifier in java"  
description: "Program which contains private specifier for specific function or class or datatype, it can be accessible only for that class or function. It is not a"  
author: "zack mathews"  
published: 2016-10-05  
updated: 2018-03-16  
canonical: https://www.mindstick.com/blog/11215/access-specifier-and-modifier-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Access specifier and modifier in java

**There are 4 types of access specifier in java:**\

- Private

- public

- protected

- default

## Private:

Program which contains private specifier for specific [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) or class or

\

[datatype](https://www.mindstick.com/forum/32/stored-procedure-return-datatype), it can be accessible only for that class or function. It is not

\

accessible by other class or assemblies.

## Public:

Program which contains public specifier for function or class or datatype, it can be accessible anywhere within the program or in other assemblies also.

## Protected:

Program in which in protected specifier is specified for function or class or datatype, it can be accessible only for that class or function. It is not accessible by other class or assemblies. But it can be accessible in the [derived class](https://answers.mindstick.com/qa/30686/what-is-a-base-class-and-derived-class).

## Default:

It is similar to [INTERNAL](https://www.mindstick.com/interview/773/describe-the-accessibility-modifier-protected-internal) access specifier in C#. Program in which in no specifier is specified for function or class or datatype then default specifier is it can be used anywhere within the program. But it is not accessible by other assemblies.

**Public**

class access

{

public String Stra;

[public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) print()

{

System.out.println("[MindStick](https://yourviews.mindstick.com/view/84668/how-mindstick-is-used-for-marketing-purposes) is better than" + Stri);

}

}

class Program

{

static void Main(String[] args)

{

access sp = new access();

System.out.println ("Enter the name of the company :");

sp.Stri = System.console().readLine();

sp.print();

}

}

}

**Private**

```
class access
```

{

**private** String Strp;

public void print()

{

System.out.println ("MindStick is better than " + Stri);

}

}

class Program

{

static void Main(String[] args)

{

access sp = new access();

System.out.println ("Enter the name of the company:");

sp.Stri = System.console().readLine();**// error will occur**

sp.print();

}

}

}

## Program for Protected Specifier

class access\
{

Protected String Stri;

public void print()

{

System.out.println ("MindStick is better than " + Stri);

}

}

class Program

{

static void Main(String[] args)

{

access sp = new access();

System.out.println ("Enter the name of the company:");

sp.Stri = System.console().readLine();**// error will occur**

sp.print();

Console.ReadLine();

}

}

\
\

## Program for Default Specifier:

```
 class access
```

{

String Stri;

public void print()

{

System.out.println ("MindStick is better than " + Stri);

}

}

class Program

{

static void Main(String[] args)

{

access sp = new access();

System.out.println ("Enter the name of the company:");

sp.Stri = System.console().readLine();

sp.print();

Console.ReadLine();

}

}

**Whereas in C# there are 5 access specifier:**

Private

\

Public

\

Protected

\

Internal

\

Protected Internal

\

**Check this related [articles](https://www.mindstick.com/articles/12872/how-to-write-articles-of-50-and-more-a-day-quick-and-easy) & blogs**:

\
[Basic concept for access specifier](https://www.mindstick.com/Blog/11203/basic-concept-for-access-specifier)

\

[Access Specifier in java](https://www.mindstick.com/Articles/748/access-specifier-in-java)

[Access Modifier in java](https://www.mindstick.com/Blog/10872/access-modifiers-in-java)

---

Original Source: https://www.mindstick.com/blog/11215/access-specifier-and-modifier-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
