Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > global veriable in same package

Reply
Thread Tools

global veriable in same package

 
 
Peter
Guest
Posts: n/a
 
      10-24-2003
Hi
As topic, i want to create "ONE" global variable, all the classes
within the same package can use it and share it. Able?

thanks
from Peter ((E-Mail Removed))
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      10-24-2003
Peter wrote:
> Hi
> As topic, i want to create "ONE" global variable, all the classes
> within the same package can use it and share it. Able?
>
> thanks
> from Peter ((E-Mail Removed))


Absolutely.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-24-2003
Peter wrote:
> 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.

 
Reply With Quote
 
Nils O. =?iso-8859-1?Q?Sel=E5sdal?=
Guest
Posts: n/a
 
      10-24-2003
In article <(E-Mail Removed) >, Peter wrote:
> 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.)
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      10-24-2003
Knute Johnson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Peter wrote:
> > Hi
> > As topic, i want to create "ONE" global variable, all the classes
> > within the same package can use it and share it. Able?
> >
> > thanks
> > from Peter ((E-Mail Removed))

>
> Absolutely.


put it in where?
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      10-24-2003
Knute Johnson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Peter wrote:
> > Hi
> > As topic, i want to create "ONE" global variable, all the classes
> > within the same package can use it and share it. Able?
> >
> > thanks
> > from Peter ((E-Mail Removed))

>
> Absolutely.


How? don't ask me to ask a smart question
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-24-2003
On 23 Oct 2003 19:35:32 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (Peter) wrote or
quoted :

> 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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      10-24-2003
Michael Borgwardt wrote:
> Peter wrote:
>
>> 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.
>


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.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-24-2003
Knute Johnson wrote:

>>> 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.
>>

>
> 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.

 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      10-25-2003
Michael Borgwardt wrote:

> Knute Johnson wrote:
>
>> 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.


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.

>> And for constants it makes a lot of sense.

> 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?

--

Knute Johnson
email s/nospam/knute/
Molon labe...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Symbols and class veriable Jerome David Sallinger Ruby 4 02-07-2010 10:28 PM
Help me about nil check of veriable( question of a niw bie) Sasaki Ruby 2 03-27-2007 02:07 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
how to get the data type of a veriable sunil.reloaded@gmail.com C Programming 7 07-18-2005 01:48 PM
package module import name clash with global package George P Python 3 09-11-2004 01:19 PM



Advertisments