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

  1: public int 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

  1: private int 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.

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

  1: System.out.println("My program is " +args[0] );

will accept one command argument from the user .

You can execute the program by entering the

  1: java Work verygood1 verygood2

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

  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.

  1: class CommandLine {
  2: public static void main(String args[]) {
  3: int x; int sum;
  4: for(x = 0; x<args.length;x++) {
  5: sum += Integer.parseInt (args[i]);
  6: System.out.println("Sum of Integers is " +sum);
  7: }
  8: }
  9: } 

One Response

  1. Peng Xu

Leave a Comment