global veriable in same package

M

Michael Borgwardt

Peter said:
As topic, i want to create "ONE" global variable, all the classes
within the same package can use it and share it. Able?

Yes. But very bad design unless it's a constant, which you do in Java
by putting a static final field with default visibility into some
class or interface.
 
N

Nils O. =?iso-8859-1?Q?Sel=E5sdal?=

Hi
As topic, i want to create "ONE" global variable, all the classes
within the same package can use it and share it. Able?
As there is no concept of "Global" variables in Java, you can
do something like:
final class Globals {
static int foo = 1;
static String fooString = null;

}

(you only problem might occur if some of your classes are loaded
by unrelated classloaders.)
 
R

Roedy Green

As topic, i want to create "ONE" global variable, all the classes
within the same package can use it and share it. Able?
Just make in static in some class.
It is then unique within the JVM.
 
K

Knute Johnson

Michael said:
Yes. But very bad design unless it's a constant, which you do in Java
by putting a static final field with default visibility into some
class or interface.

Why would that be bad design says the guy who has done that on occasion?
It's a pain in the neck to have to pass in variables that get used
everywhere. And for constants it makes a lot of sense.
 
M

Michael Borgwardt

Knute said:
Why would that be bad design says the guy who has done that on occasion?
It's a pain in the neck to have to pass in variables that get used
everywhere.

There should *be* no variables that get used everywhere; good OO design should
(and usually can) ensure that variables and the code that uses them are
encapsulated together in classes so that they don't have to be exposed
widely. At the very least, access should be through getter and setter
methods so that you can at least do some checks.

Your comment about "passing in" variables that get used everywhere seems
to indicate that you're thinking procedurally, not object oriented.
And for constants it makes a lot of sense.

I said that it was bad desing *unless* it's a constant.
 
K

Knute Johnson

Michael said:
There should *be* no variables that get used everywhere; good OO design
should
(and usually can) ensure that variables and the code that uses them are
encapsulated together in classes so that they don't have to be exposed
widely. At the very least, access should be through getter and setter
methods so that you can at least do some checks.

Getter and setter methods make a lot of sense if you have to control
access to the fields. Java has a few fields that are public and that is
a very convenient way of access them.
Your comment about "passing in" variables that get used everywhere seems
to indicate that you're thinking procedurally, not object oriented.

Us dinosaurs do have a bit of trouble with OO and non-linear programming.
I said that it was bad desing *unless* it's a constant.

I just wrote a program where I needed to set some variables when the
program starts and then use them while the program runs. It made
perfect sense to me to keep those with the rest of the final statics.
This same program had 10 classes each running a thread that needed to be
synchronized during file I/O and data access. The data was in one
class, I/O was in one class, and the Runnable class read the files and
updated the data. Probably one too many classes there but the sync
objects were in a final static array. All class could easily get at
them. Other than combining the data and I/O into one class, how else
could you share an object between the classes?
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top