global veriable in same package

Discussion in 'Java' started by Peter, Oct 24, 2003.

  1. Peter

    Peter Guest

    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 ()
     
    Peter, Oct 24, 2003
    #1
    1. Advertising

  2. 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 ()


    Absolutely.

    --

    Knute Johnson
    email s/nospam/knute/
    Molon labe...
     
    Knute Johnson, Oct 24, 2003
    #2
    1. Advertising

  3. 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.
     
    Michael Borgwardt, Oct 24, 2003
    #3
  4. In article <>, 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.)
     
    Nils O. =?iso-8859-1?Q?Sel=E5sdal?=, Oct 24, 2003
    #4
  5. Peter

    Peter Guest

    Knute Johnson <> wrote in message news:<>...
    > 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 ()

    >
    > Absolutely.


    put it in where?
     
    Peter, Oct 24, 2003
    #5
  6. Peter

    Peter Guest

    Knute Johnson <> wrote in message news:<>...
    > 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 ()

    >
    > Absolutely.


    How? don't ask me to ask a smart question
     
    Peter, Oct 24, 2003
    #6
  7. Peter

    Roedy Green Guest

    On 23 Oct 2003 19:35:32 -0700, (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.
     
    Roedy Green, Oct 24, 2003
    #7
  8. 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...
     
    Knute Johnson, Oct 24, 2003
    #8
  9. 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.
     
    Michael Borgwardt, Oct 24, 2003
    #9
  10. 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...
     
    Knute Johnson, Oct 25, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George P
    Replies:
    3
    Views:
    695
    Alex Martelli
    Sep 11, 2004
  2. Replies:
    7
    Views:
    298
    Krzysztof Olczyk
    Jul 18, 2005
  3. della
    Replies:
    6
    Views:
    338
    Peter Otten
    Nov 28, 2008
  4. Sasaki
    Replies:
    2
    Views:
    104
    ChrisH
    Mar 27, 2007
  5. Jerome David Sallinger

    Symbols and class veriable

    Jerome David Sallinger, Feb 7, 2010, in forum: Ruby
    Replies:
    4
    Views:
    110
    Albert Schlef
    Feb 7, 2010
Loading...

Share This Page