File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lazy initialization

 
Othmar Stehlik
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,
There r too many public classes/interface to be compiled..but nice explanation!
------------------
azaman
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Hi David!
The idea was there in my example, you just make it less generalized.

------------------
Antti Barck
It Solutions Consultant -- NSD Oy
(SCJP pending)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic