The Artima Developer Community
Sponsored Link

Java Answers Forum
generic java programming questions

2 replies on 1 page. Most recent reply: Jan 16, 2004 8:09 AM by Vincent O'Sullivan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Gb

Posts: 2
Nickname: dketch
Registered: Jan, 2004

generic java programming questions Posted: Jan 15, 2004 8:34 AM
Reply to this message Reply
Advertisement
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


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: generic java programming questions Posted: Jan 16, 2004 3:45 AM
Reply to this message Reply
Given a generic java programming question, please forgive the generic java programming answer: it's down to personal taste.

Adam

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: generic java programming questions Posted: Jan 16, 2004 8:09 AM
Reply to this message Reply
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

Flat View: This topic has 2 replies on 1 page
Topic: Polutating JTable Previous Topic   Next Topic Topic: custom frames

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use