Why do I need 2 statements for instantiation vs. 1?
Janet Wilson
Ranch Hand
Joined: Jul 16, 2002
Posts: 98
posted
0
I was reading this great article re: Factory Methods from Javaworld: Factory Methods when I stumbled upon their code which I have cut and pasted here because I am experiencing a problem with it. My question is why do I need to use 2 statements for instantiation in my TraceExample.java because I can't get it to work with just the 1 which was provided in the article? Also, by using 2 stmts, will that ruin the Factory Method pattern (figured I'd sneak that question even if it might belong in the OO category). Here is the Trace.java....
Here is the SystemTrace.java...
Here is the TraceFactory.java....
And lastly, the TraceExample.java....
Thanks folks! It's probably one of those end of the work week types of things which is blinding me to the solution. Janet [ December 13, 2002: Message edited by: Janet Wilson ]
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
hi Janet,
Trace log = new TraceFactory.getTrace();<==This line does not work if lines 6 & 7 are commented--why?
here i observe one thing. u r using, new TraceFactory.getTrace(); why u have put "new"? the object is returned from TraceFactory right? OR if that was a type then i am not sure what is causing the problem. regards maulin
Thanks Maulin! I removed the new and changed the commented lines and it works!
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
Of course you could also have called the method on a factory instance, but then you had to complete the constructor call with parentheses: Trace log = new TraceFactory().getTrace();
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Janet Wilson
Ranch Hand
Joined: Jul 16, 2002
Posts: 98
posted
0
You are right - this works, too. The author probably meant this. Thanks Ilja!