Java Access Modifiers/Specifiers :

An access modifier restricts the access of a class, constructor, data member and method in another class. In java we have four access modifiers:
1. Default
2. Private
3. Protected
4. Public

Default Access Modifier:

Default Access Modifier is otherwise called as Package Level Access Modifier.

If you don’t use any modifier, it is treated as default by default. The default modifier is accessible only within package. It cannot be accessed from outside the package. It provides more accessibility than private. But, it is more restrictive than protected, and public.

How to import Java Package?

To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program.

Examples:

OFFICE PROGRAM:

package tn.chennai;

public class Office
{
String landmark =”Tambaram Post Office”;
int doorno = 123;
static void main(String[]args)
{
Office emp1 = new Office();
System.out.println(emp1.doorno);
System.out.println(emp1.landmark);
}
public void attendonlineMeetings()
{
System.out.println(“Online Meetings”);
}
void MeetatOffice()
{
System.out.println(“Office Meetings”);
}

}

OUTPUT:

  • c:\java>javac -d . Office.java
  • c:\java>java tn.chennai.Office
    123
    Tambaram Post Office

EMPLOYEE PROGRAM:

package tn.madurai;
import tn.chennai.Office;
public class Employee
{
public static void main(String[]args)
{
Office emp1 = new Office();
System.out.println(emp1.landmark);
System.out.println(emp1.doorno);
emp1.attendonlineMeetings()
emp1.MeetatOffice();
}

}

OUTPUT:

c:\java>javac -d . Employee.java
Employee.java:8: error: landmark is not public in Office; cannot be accessed from outside package
System.out.println(emp1.landmark);
^
Employee.java:9: error: doorno is not public in Office; cannot be accessed from outside package
System.out.println(emp1.doorno);
^
Employee.java:11: error: MeetatOffice() is not public in Office; cannot be accessed from outside package
emp1.MeetatOffice();
^
3 errors

EMPLOYEE2 PROGRAM:

package tn.chennai;

public class Employee2
{
public static void main(String[]args)
{
Office emp1 = new Office();
System.out.println(emp1.landmark);
System.out.println(emp1.doorno);
emp1.attendonlineMeetings();
emp1.MeetatOffice();
}

}

OUTPUT:

  • c:\java>javac -d . Employee2.java
  • c:\java>java tn.chennai.Employee2
    Tambaram Post Office
    123
    Online Meetings
    Office Meetings

JAVA STATIC METHOD:

The Static Keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

If you declare any variable as static, it is known as a static variable.

Advantages of static variable:

It makes your program memory efficient (i.e., it saves memory).

Eamples:

INDIA Program:

public class India
{
static String authority = “ECI”;
String citizen_name;
public India(String citizen_name)
{
this.citizen_name = citizen_name;
}

public static void main(String[]args)
{
India c1 = new India(“Singaram”);
System.out.println(c1.citizen_name);
System.out.println(India.authority);
India c2 = new India(“Vimal”);
System.out.println(c2.citizen_name);
}
}

OUTPUT:

c:\java>java India
Singaram
ECI
Vimal
ECI

Leave a comment

Design a site like this with WordPress.com
Get started