Private Access Specifier Example Java Program


Definition

Access specifiers are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components. The visibility of the Private access specifier is limited to only for a class where it is defined.

Syntax

private class class_name{
	//Do something
}
OR
private method_name(){
	//Do something
}

Private Access Specifier Example Program

public class PrivateAccessSpecifier {
	private static String PrivateMethod(){
		return "This is inside a private method";
	}
	public static void main(String[] args) {
		System.out.println("" + PrivateMethod());
	}
}

Sample Output

Output is:
This is inside private method