File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Lazy initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Lazy initialization" Watch "Lazy initialization" New topic
Author

Lazy initialization

Othmar Stehlik
Ranch Hand

Joined: Jul 24, 2001
Posts: 36
Hi!
A question for an exercise from Thinking in Java:
Make a simple class, inside another class define a field for an object of the first class. Use lazy nitializazion for instantiating the this object:
I wrote the following code, which works, but I don�t think its lazy initializazion:
class Simple{

Simple(String s){
System.out.println("String args");
}
Simple(int i){
System.out.println("Int args");
}
Simple(String s, int i){
System.out.println("String and int args");
System.out.println("String =" + s + "int =" + i);
}
Simple(boolean b){
this("Hi", 99);
System.out.println("I have boolean args");
}
}
public class Simpler extends Simple{
Simpler(){
super("Hallo", 11);
System.out.println("No args");
}
public static void main(String[] args){
Simpler s = new Simpler();
}
}
Can anybody tell me what lazy initialization means?

[This message has been edited by Othmar Stehlik (edited July 25, 2001).]
[This message has been edited by Othmar Stehlik (edited July 25, 2001).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
As I see it, by the definition "Make a simple class, inside another class define a field for an object of the first class." the code might be like this:

I dont know what lazy initialization is...

------------------
Antti Barck
It Solutions Consultant, NSD Oy
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think this would qualify:

This however has something to do with the patterns and not directly related to the Exam.
Correct if I am wrong.

------------------
Antti Barck
It Solutions Consultant, NSD Oy
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
So did it qualify?
And what are the drawbacks for this pattern?
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I am using terminology from the book "Design Patterns"
Antti, I you have the right idea, but it's not quite right. The Singleton implementation you show isn't actually lazy -- the Simple is created right when you ask for it, not when you need it/when it's ready.
Lazy initialization is done with the Proxy pattern. The proxy pattern has many uses, and lazy initialization is just one of them. Rather than explain it, I'll give an example:
Othmar Stehlik
Ranch Hand

Joined: Jul 24, 2001
Posts: 36
Hi Antti,
Hi David,
Thanx for your explantations, although Design Patterns are not part of SCJ2P Exam I�m glad to know this stuff,

[This message has been edited by Othmar Stehlik (edited August 07, 2001).]
[This message has been edited by Othmar Stehlik (edited August 07, 2001).]
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi David,
There r too many public classes/interface to be compiled..but nice explanation!
------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi David!
The idea was there in my example, you just make it less generalized.

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lazy initialization