• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Access Modifiers in Java

January 18, 2014 //  by Krishna Srinivasan//  Leave a Comment

This tutorials explains the various access modifiers used in Java language and the scope for the each modifier. There is four types of access modifiers in Java.

  • private
  • default
  • protected
  • public

Private Modifier

If you declare anything private inside a class, then it must be accessed inside that class. Other classes can not access that variables or methods directly.

  • We can not declare a class as private
  • Variables and methods can be declared as private.
  • If you declare a constructor as private, we can not create instance using that constructor.
  • Private is the least access modifier.
package javabeat.net.core;

public class SuperClass {
	private int i;
	private SuperClass(){
		//Private constructor
	}
	private void method(){
		//Private method
	}
}

Default Modifier

If you don’t declare access modifier, then it is considered as the default access. It means that the access is granted within the same package.

  • Default access has no keyword. Without any access modifier is considered as default access.
  • This access is applicable for classes, methods and variables.
package javabeat.net.core;

public class SuperClass {
	int i;
	SuperClass(){
		//Default constructor
	}
	void method(){
		//Default method
	}
}
package javabeat.net.core;

public class OtherClass {
	void method(){
		SuperClass class1 = new SuperClass();
		System.out.println(class1.i);
	}
}

Protected Modifier

If you declare a method or variable as the protected, it can be accessed within the same package and sub classes in the other packages.

  • Classes can not be declared as protected access.
  • Variables and methods can be declared as protected.
  • The main purpose of the protected is to give access for its sub classes.
package javabeat.net.core;

public class SuperClass {
	protected int i;
	protected SuperClass(){
		//protected constructor
	}
	protected void method(){
		//protected method
	}
}

package javabeat.net.core;

public class SubClass extends SuperClass{
	protected void method (){
		SuperClass class1 = new SuperClass();
		System.out.println(class1.i);
	}
}

Public Modifier

If you declare as public, then the data member can be accessed from anywhere .

  • Classes, variables and methods can be public.
  • It is the maximum access level

Summary of Access Level

Modifier Inside Class Inside Package Other Package by Subclass Only Other Package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y

Category: JavaTag: Java Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « OCPJP 6 Mock Exam -10
Next Post: Instance Initializer in Java »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact