Public 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 public accesses specifier gives access to the member function from outside the class. This part allows the programmer of a class to easily change the data member of the class.

Syntax

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

Public Access Specifier Example Program

class Main{  
	public int num=789;  
	public void data(){
		System.out.println("She sells sea shells on the sea shore");
	}  
}  
class PublicAccessSpecifier{  
	public static void main(String args[]){  
		Main obj=new Main();  
		System.out.println(obj.num);
		obj.data();
	}  
}  

Sample Output

Output is:
789
She sells sea shells on the sea shore