Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do I need 2 statements for instantiation vs. 1?

 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maulin!
I removed the new and changed the commented lines and it works!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Janet Wilson
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right - this works, too. The author probably meant this. Thanks Ilja!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic