Sie sind auf Seite 1von 18

Programming in Java

java enum, Singleton Pattern and instanceOf

By
Arvind Kumar
Asst. Professor, LPU
Enum Types
• An enum type is a special data type that enables for a variable
to be a set of predefined constants.
• The variable must be equal to one of the values that have been
predefined for it.
• enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY;
}
• The identifiers SUNDAY, MONDAY, and so on, are called
enumeration constants.
• Each is implicitly declared as a public, static final member of
Day.
Enum Types
• A Java enumeration is a class type. Although you don’t
instantiate an enum using new.
• Once you have defined an enumeration, you can create a
variable of that type :
Day d;
• Because d is of type Day, the only values that it can be
assigned (or can contain) are those defined by the
enumeration.
d = Day. MONDAY ;
• Two enumeration constants can be compared for equality by
using the = = relational operator. For example,
• Note: An enumeration value can also be used to control a
switch statement.
Enum Types
• Java Enumerations are Class Types

• The enum declaration defines a class (called an enum type).


The enum class body can include methods and other fields.

• It is important to understand that each enumeration constant is


an object of its enumeration type.
Enum Types
• The values( ) and valueOf( ) Methods
All enumerations automatically contain two predefined
methods: values( ) and valueOf( ). Their general forms are
shown here:

• public static enum-type [ ] values( )


The values( ) method returns an array that contains a list of
the enumeration constants.

• public static enum-type valueOf(String str )


The valueOf( ) method returns the enumeration constant
whose value corresponds to the string passed in str.
• In both cases, enum-type is the type of the enumeration.
Java Singleton Pattern
• Singleton pattern restricts the instantiation of a class and
ensures that only one instance of the class exists in the java
virtual machine.

• The singleton class must provide a global access point to get


the instance of the class.
Java Singleton Pattern
To implement Singleton pattern common concepts are:

• Private constructor to restrict instantiation of the class from


other classes.
• Private static variable of the same class that is the only
instance of the class.
• Public static method that returns the instance of the class, this
is the global access point for outer world to get the instance of
the singleton class.
Java Singleton Pattern
public class SingleObject {
//create an object of SingleObject
private static SingleObject instance = new SingleObject();

//make the constructor private so that this class cannot be instantiated


private SingleObject(){}

//Get the only object available


public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}
Java Singleton Pattern
public class SingletonPatternDemo {
public static void main(String[] args) {

//illegal construct
//Compile Time Error: The constructor SingleObject() is not visible
//SingleObject object = new SingleObject();

//Get the only object available


SingleObject object = SingleObject.getInstance();

//show the message


object.showMessage();
}
}
instanceof
• The java instanceof operator is used to test whether the object
is an instance of the specified type (class or subclass or
interface).

• The instanceof in java is also known as type comparison


operator because it compares the instance with type.

• It returns either true or false.

• If we apply the instanceof operator with any variable that has


null value, it returns false.

Das könnte Ihnen auch gefallen