This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why do I need 2 statements for instantiation vs. 1? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why do I need 2 statements for instantiation vs. 1?" Watch "Why do I need 2 statements for instantiation vs. 1?" New topic
Author

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

Janet Wilson
Ranch Hand

Joined: Jul 16, 2002
Posts: 98
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: 1871
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

Joined: Jul 16, 2002
Posts: 98
Thanks Maulin!
I removed the new and changed the commented lines and it works!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
You are right - this works, too. The author probably meant this. Thanks Ilja!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do I need 2 statements for instantiation vs. 1?
 
Similar Threads
a labeled break outside of an if statement
Factory Method pattern
Problem using EclipseLink -> org.springframework.dao.InvalidDataAccessApiUsageException
Gmail and port 465 vs. port 587
while(false) vs. if(false)