12:20 am - Monday June 26, 2017

The Collection Program Silgleton Returns an immutable set containing only the specified object using singleton(T o) method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

 

Returns an immutable set containing only the specified object. It restricts the instantiation of a class to one object.

Example Program

public class Singleton {
	private static Singleton sing = new Singleton();

	/*
	 * A private Constructor prevents any other class from instantiating.
	 */
	private Singleton() {
	}

	public static Singleton getInstance() {
		if (sing == null) {
			sing = new Singleton();
		}
		return sing;
	}

	protected static void sample() {
		System.out.println("It's a sample method for singleton");
	}
}

Output:

It’s a sample method for singleton

 Explanation:

Returns an immutable set containing only the specified object. It restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

Parameters:

o – the sole object to be stored in the returned set.

Returns:

An immutable set containing only the specified object.

 

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.