Lazy activation

Can some one help me in understanding what lazy activation is?Thankx
[89 byte] By [ganeshramhere] at [2007-9-30 18:50:51]
# 1

Lazy activation involves waiting to activate something until it is first used. It's similar to lazy instantiation where instead of creating an instance in advance, you wait until someone actually requests the instance.

For example:class Instantiator {

private Object eagerMember; // we'll use eager instantiation for this one

private Object lazyMember; // we'll use lazy instantiation for this one

public EagerInstatiator() {

// instantiate eager member as soon as this object is created

eagerMember = new String("Big string taking up memory and time upon creation of this");

}

public Object getEagerMember() {

// return immediately, the member is already available

return member1;

}

public Object getLazyMember() {

// instantiate lazyMember only when this method gets called for the first time

if(lazyMember == null) {

lazyMember = new String("Newly created string taking up memory and time upon first request");

}

return lazyMember;

}

}

Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 2
I forgot to mention that lazy *activation* is the term used for lazy instantiation of objects through Remote Method Invocation.
Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 3
Even in the case of the case of eager instantiation the instance is created only after calling the EagerInstatiator() method. I think it should have been the constructor of the class InstantiatorAm i right
ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 4
Whoops, yeah that's a typo, EagerInstantiator should be Instantiator (when I started to type the example I was planning to do two classes EagerInstantiator and LazyInstantiator. I forgot to change the name afterwards).
Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 5
Cool man . So is there any relation ship between Lazi instance and single ton object .bcoz even in this case the object can be created only once. I mean only if the instance already null.
ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 6

Singletons are often lazily instatiated like this:class LazySingleton {

private static LazySingleton instance;

private LazySingleton() {}

public static getInstance() {

if(instance == null) {

instance = new LazySingleton();

}

return instance;

}

Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 7

I think that u r enforcing the Singleton type of situatin using

public Object getLazyMember() {

// instantiate lazyMember only when this method gets called for the first time

if(lazyMember == null) {

lazyMember = new String("Newly created string taking up memory and time upon first request");

}

Butall u have to check is wether " this method gets called for the first time" and for that y u check "lazyMember == null" offcousre only if the method if called for the first time the lazyMember will be null but here single ton kind of situation gets unnecessarily implemented .

As per ur idea lazy instantiation some thing which will happen only during first time methid call r can be said as "instanciated only wen needed" (some thing like late binding) but using if(lazyMember == null) makes it so that U can create only one instance.

Is this rite?

ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 8

I'm not entirely sure what you mean. First of all there is no way to check if a method gets called for the first time. You need to keep track of this yourself. In my examples, I use the fact that I know lazyMember and instance will be null before the first call, and not null after. I could also have use a boolean to indicate that the method had not been called yet, but this way I have the added benefit of ensuring a non-null return value.

The first example does not implement a singleton pattern, just lazy instantiation of a member variable. The example use a single member variable that gets instantiated only once, otherwise it wouldn't be lazy instantiation, but just a method returning a new object every time.

Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 9
"The example use a single member variable that gets instantiated only once" But this wat a sigleton also does?so no difference am i right?
ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 10
hey sorry sorry i got the pointThanks a lot man
ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 11
One more thing Why and wer we use lazy instantiation.
ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 12

> "The example use a single member variable that gets

> instantiated only once" But this wat a sigleton also

> does?

I'm sorry, I meant to say instance variable (or actually instance member). A singleton uses a class variable.

> so no difference am i right?

Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 13

I generally use it for two reasons:

1. faster startup times. If you instantiate all the variables your program is ever going to need right away, it could take a long time for your application to start up. On a smaller scale: all object creation takes time, which is why you want to avoid creating objects you don't need right away.

2. memory usage. Instantiated objects take up space in memory. If you don't need an object right away (or at all), you save memory by not instantiating it.

Herko_ter_Horst at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...
# 14
Hi Thaku very much.And not stoppiong with that i know that u desrve some duke's dollars here it is..........................................
ganeshramhere at 2007-7-6 21:08:27 > top of Java-index,Administration Tools,Sun Connection...