Understanding Access Specifiers in Java

The scope of variables can be accessed by Access specifiers in Java. There are four levels of access specifiers in Java. They are Public, Private and Protected.

Let us now examine each concept in detail:

PUBLIC: If the method or the variable is declared public, then it will be visible to all classes of the program. For example, if you declared the variable named x as public

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: public <span style="color: #0000ff">int</span> x = 20;


then the value of x will be visible to all the other classes of the same program.

PRIVATE: If the method or the variable is declared private , then it will be visible only to the class it is defined. For example

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: private <span style="color: #0000ff">int</span> y = 100;


means the value of y will be visible only to the class it is defined. If you try to access the variable in other classes then the compiler will return an error.

PROTECTED: This is same as Public but it will give more protection than the public. If Protected is used then all the methods and attributes of the base class will be available to all its derived classes.

Concept of Packages and Java Class Libraries

Packages are a set of classes built into Java Development Kit . All the classes in Java.lang package are automatically imported when the program runs. The rest of other classes in the packages are to be imported by the programmer by using the import keyword. Following table shows commonly used java packages.

READ  Interview with Jennifer Castillo on Cognitier HomeWeb Service

Name of the Package Description

java.lang –> String, Data Type classes

java.util  –> Date, Time classes etc

java.io    –> Files and Streams handler

java.net  –> Networking ,URL etc

java.awt  –> GUI classes

java.applet –> Applet classes, Audioclip Interface etc

Except java .lang Package all other classes are to be imported by import keyword. To import date class in java .util package, you have to use the statement import java.util.Date;

To import all the classes in the java.io package use import.java.io.* . The * will import all its classes automatically.

Command Line Arguments

You can pass arguments to java programs during run time. For instance, the code

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: System.out.println(&quot;<span style="color: #8b0000">My program is </span>&quot; +args[0] );

will accept one command argument from the user .

You can execute the program by entering the

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: java Work verygood1 verygood2

The Work denotes class name. The output of this program will be

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: My program is very good1 verygood2

This is an alternative to the keyboard input commands like cin in C++ and scanf in C

Following example helps you to understand this concept better. It accepts values from the user via the command line and computes the sum of the values. Since, command line arguments are passed as Strings, it is necessary to convert the values into Integers through parseInt() function. Since parseInt() is a class method, it’s accessed via the class name.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> CommandLine {
  2: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[]) {
  3: <span style="color: #0000ff">int</span> x; <span style="color: #0000ff">int</span> sum;
  4: <span style="color: #0000ff">for</span>(x = 0; x&lt;args.length;x++) {
  5: sum += Integer.parseInt (args[i]);
  6: System.out.println(&quot;<span style="color: #8b0000">Sum of Integers is </span>&quot; +sum);
  7: }
  8: }
  9: } 

One Response to "Understanding Access Specifiers in Java"

  1. Peng Xu   May 29, 2011 at 11:14 pm

    Nice and easy to understand, thanks.

    Reply

Leave a Reply

Your email address will not be published.