|
Re: generic java programming questions
|
Posted: Jan 16, 2004 8:09 AM
|
|
There's no significant difference in the two versions of both examples - in their current form - however, there may be some implications when you modify the code in the future.
1) In the first example b is initialized when the class is instantiated. In the second example it is not initialised until method A has been called called. This may have implications if you introduce a new method A1 (that uses b).
2) Again, the implications mainly apply to future modifications. If the return value of a.getB() is simple, dynamic and/or not used much then calling the method each time the value is needed is fine. If the return value requires a complex calculation, doesn't change between calls or is used a lot, then it may be better to make a single call to the method, store the result locally and use that local result in several places, as required.
The decision of which way to go in each case is yours.
Vince.
> hi all, > a few questions about Java programming. > > 1) Is there any difference between: > > public class A { > private B b = new B(); > > public A() { > } > } > > and > > public class A { > private B b = null; > > public A() { > b = new B(); > } > } > > ? > > 2) and between: > > c.do(a.getB()); > > and > > B b = a.get; > c.do(b); > > ? > Which one is the best syntax? > > thanks, > > d_k
|
|