question about accessors and constants and variable declaration locations

F

Flip

I'm having a discussion with someone at work and would like to know if
there are some links I can use. I remember reading, but unfortunately don't
know
any links :< of bean usage and variable declarations.

When you want to create a hard coded value, there are two ways AFAIK to get
the value.
1- create a constant with hard coded value, create an accessor and return
the constant
2-create the accessor and return the hard coded value right in the accessor

Which way do you coders do it and can you suggest any links for references?

Also, where "should" variables be declared? At the top of method or as you
use them? Java lets you do both, but what is the preferred way? I know the
VB way is you HAVE to declare them at the top before any code, but I seem to
remember reading on some Sun site they suggest declaring vars as you use
them. Which do you do in real life?


Thanks.
 
B

Boudewijn Dijkstra

Flip said:
I'm having a discussion with someone at work and would like to know if
there are some links I can use. I remember reading, but unfortunately don't
know
any links :< of bean usage and variable declarations.

When you want to create a hard coded value, there are two ways AFAIK to get
the value.
1- create a constant with hard coded value, create an accessor and return
the constant
2-create the accessor and return the hard coded value right in the
accessor

3-create a constant, make it public static final and if it's an object, also
immutable

1 is better than 2 if you want to use the constant in the declaring class
(or in the subclasses, or classes in the same package), it avoids the need
for a method call.
Which way do you coders do it and can you suggest any links for references?

Also, where "should" variables be declared? At the top of method or as you
use them? Java lets you do both, but what is the preferred way? I know the
VB way is you HAVE to declare them at the top before any code, but I seem to
remember reading on some Sun site they suggest declaring vars as you use
them. Which do you do in real life?

I usually declare constants in the top, and variables as I use them.
 
F

Flip

I usually declare constants in the top, and variables as I use them.
Thanks for your input! :> Much appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top