In-Depth Guide to Object-Oriented Programming - Part II

In-Depth Guide to Object-Oriented Programming - Part II

Java Packages

Package in Java is a mechanism to encapsulate a group of classes, sub packages and interfaces.

Packages prevent naming conflicts. There can be two classes with name Employee in two packages.

"import" Statement

Import statement in Java is helpful to take a class or all classes visible for a program specified under a package, with the help of a single statement.

"Static" in Java

In Java, a static variable is a class-level variable that is shared among all instances of the class. This means that all objects of the class access the same copy of the static variable.

Initialization

In Java, a static block is executed only once when the class is loaded into memory.

Outer classes cannot be static. They are not dependent on other classes.

Singleton Class

A java singleton class is a class that can have only one object at a time.

Make a constructor private. Write a static method that has the return type object of this singleton class.