Private Access Modifier in Java Programming

In this tutorial, you will learn about private access modifier in java programming.

You will learn what is private access specifier, how to create and use them in detail with example.

Source code for this video tutorial

Code for Student.java

package oops;

public class Student {
 
 private static int nostudents = 0;
 
 public Student(){
  nostudents++;
 }

 private String name;
 public int age;
 
 public void setName(String iname){
  name = iname;
 }
 
 public String getName(){
  return name;
 }
 
 
 private void doSomething(){
  System.out.println(name+" is doing something");
 }
 
 void introduce(){
  doSomething();
  System.out.println("name is "+name+" and age is "+age);
 }
 
 static void howManyStudents(){
  System.out.println("There are totally "+nostudents+" Students");
 }
 
}

Code for Tutorials.java

package oops;

public class Tutorials {

 public static void main(String[] args) {
  
  Student anil = new Student();
  anil.setName("Anil");
  anil.age = 24;  
  System.out.println("Name is "+anil.getName()); 
  anil.introduce();
  
  Student shreesh = new Student();
  
  Student.howManyStudents();
  
 
  Student.howManyStudents();
 }

}
Back To Top