Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
What are access modifiers in java ?
Previous Home Next

Access modifiers determines the accessibility scope of the Java elements that are being modified. If we not explicitly use an access modifier with a Java element, the element implicitly has the default access modifier.

The explicit access modifiers may be used with a class and its members. They cannot be used with the variables inside a method. The Java has three explicit access modifiers viz. public, private and protected, and also has a default modifier, which is used when you do not explicitly specify a modifier.

public modifier

The public modifier makes an element most visible, it can be applied to various classes and their members (instance variable and methods). The elements declared public can be accessed from anywhere in the java application. That is why you declare the main method of any application public so that it may be invoked from any Java runtime environment. The syntax for declaring and using public modifiers within java program can be shown as:

Example


Java Tutorialslass ModifierDemo
{
 public int i;
 public ModifierDemo(int i)
	{
     this.i=i;
	 System.out.println("the value of i is :"+i);
    }
}
[an error occurred while processing this directive]
Java Tutorialslass PublicModifier 
{
	public static void main(String[] args) 
	{
		ModifierDemo obj=new ModifierDemo(10);
	}
}

private modifier

The private modifier is used to make a java element least visible. The private modifier cannot be used for a top-level class, and can be applied only to the members of a top-level class viz. instance variables, methods. A private member of a class can only be accessed from the code inside the same class in which this member is declared. It cannot be accessed from any other class or from a subclass of the class in which it is declared.The code snippet shows the syntax for using private modifier.

Example


Java Tutorialslass ModifierDemo
 {
 private int i=10;
 public int j=5;
 }
Java Tutorialslass PublicModifier 
{
	public static void main(String[] args) 
	{
		ModifierDemo obj=new ModifierDemo();
	    System.out.println("The value of i is :"+obj.i);  
		// cause a compile error as i's defined private.
		System.out.println("The value of j is :"+obj.j);  
		// print the value of j =5
	}
}

The accessibility of i have been kept private so we can't directly access it merely by creating the object of the class in which it is defined, so it will cause a compiler error, but if we comment out this line from prog. then we will get the value of j=5 as output.

Previous Home Next