BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

What is private Keyword in Java

By Chaitanya Singh | Filed Under: java

The private keyword is an access modifier in java. It can be used for variables, methods, constructors and inner classes.

Note:

  • The private is the most restrictive modifier compared to other modifiers such as public, default and protected.
  • It cannot be applied to a class (except inner class) or an interface.
  • The private variables and methods can only be accessed within the class. No other class can access these.
  • If a class has private constructor, then the object of this class cannot be created outside this class.

Java private variable Example

This program throws an error because we are trying to access a private variable outside the class.

class MyClass
{
  private String str="I am a private variable";
}
public class JavaExample {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    System.out.println(obj.str);
  }
}

Output:

Private variable example output

Java private method Example

Let’s see, if we can access a private method outside the class in which it is declared.

class MyClass
{
  private void demo()
  {
    System.out.println("I am a private method of class MyClass");
  }
}
public class JavaExample {
  public static void main(String[] args) {
    MyClass obj = new MyClass();
    obj.demo();
  }
}

Output:

Java private method example output

Java private class Example

In this example, we have declared a class private. As you can see that the program didn’t run fine and threw a compilation error. This is because a class (except an inner class) cannot have a private access specifier.

private class JavaExample {
  void demo(){
    System.out.println("Hello");
  }
  public static void main(String[] args) {
    JavaExample obj = new JavaExample();
    obj.demo();
  }
}  

Output:

private class Example output

Java private constructor Example

Here, we have a class MyClass that has a private default constructor. We are trying to create an object of this class in another class JavaExample. This threw a compilation error because you cannot create an object of a class with private constructor, outside the class.

class MyClass{
  //private constructor
  private MyClass(){
    System.out.println("Private constructor of MyClass");
  }
}
public class JavaExample {
  public static void main(String[] args) {
    //creating an object of MyClass
    MyClass obj = new MyClass();
  }
}  

Output:

private constructor example output

How to access private fields of class outside the class

We use getter and setter methods.

class Teacher {
  private String designation = "Teacher";
  private String collegeName = "Beginnersbook";
  public String getDesignation() {
    return designation;
  }
  protected void setDesignation(String designation) {
    this.designation = designation;
  }
  protected String getCollegeName() {
    return collegeName;
  }
  protected void setCollegeName(String collegeName) {
    this.collegeName = collegeName;
  }
  void does(){
    System.out.println("Teaching");
  }
}

public class JavaExample extends Teacher{
  String mainSubject = "Physics";
  public static void main(String args[]){
    JavaExample obj = new JavaExample();
    /* Note: we are not accessing the data members
     * directly we are using public getter method
     * to access the private members of parent class
     */
    obj.setCollegeName("My College");
    obj.setDesignation("My Teacher");
    System.out.println(obj.getCollegeName());
    System.out.println(obj.getDesignation());
    System.out.println(obj.mainSubject);
    obj.does();
  }
}

Output:

My College
My Teacher
Physics
Teaching

Recommended Posts

  • Java Access Modifiers Tutorial
  • Java Constructor Tutorial
  • Java Inner Class Tutorial
  • Java 9 – private methods in Interfaces
❮ Java Keywords

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap