Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Lazy initialization

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic