-->

Private Access Modifier in Java Programming

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

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

Source Code for Source Code for Student.java

 1 package oops;
 2 
 3 public class Student {
 4  
 5      private static int nostudents = 0;
 6      
 7      public Student(){
 8       nostudents++;
 9      }
10 
11      private String name;
12      public int age;
13      
14      public void setName(String iname){
15       name = iname;
16      }
17      
18      public String getName(){
19       return name;
20      }
21      
22      
23      private void doSomething(){
24       System.out.println(name+" is doing something");
25      }
26      
27      void introduce(){
28       doSomething();
29       System.out.println("name is "+name+" and age is "+age);
30      }
31      
32      static void howManyStudents(){
33       System.out.println("There are totally "+nostudents+" Students");
34      }
35      
36 }


Source Code for Source Code for Tutorials.java

 1 package oops;
 2 
 3 public class Tutorials {
 4     public static void main(String[] args) {  
 5         Student anil = new Student();
 6         anil.setName("Anil");
 7         anil.age = 24; 
 8 
 9         System.out.println("Name is "+anil.getName()); 
10 
11         anil.introduce();  
12 
13         Student shreesh = new Student();  
14         Student.howManyStudents(); 
15     }
16 }

Watch this video to learn how this works