Styles of programming

Discussion in 'Java' started by Thomas Korsgaard, Dec 11, 2005.

  1. Hi,

    I'm doing a project in school in Java, and I've come a cross several
    ways of programming, when dealing with classes. E.g. I have an Inventory
    class, that obviously holds a lot of data about what is in the inventory.

    When I deal with my main program i find that I can deal with it in two
    ways. Either

    Inventory inv = new Inventory();
    inv.status();
    inv.findLowStock();

    or I can acces my Inventory "directly" by:

    Inventory.status();
    Inventory.findLowStock();

    Both seem to be working fine, but what is the difference? And more
    important what is the best thing to do (I like good marks :))?

    Thanks,
    /Thomas
     
    Thomas Korsgaard, Dec 11, 2005
    #1
    1. Advertising

  2. Thomas Korsgaard

    gilles Guest

    Hi,

    "status" and "findLowStock" are certainly static methods, aren't they ?

    Static methods or data are shared by all instances of a same class.
    Instantiated objects will have their own versions of other data and methods,
    but only a single copy of the ones defined as static.

    gil


    Thomas Korsgaard wrote:

    > Hi,
    >
    > I'm doing a project in school in Java, and I've come a cross several
    > ways of programming, when dealing with classes. E.g. I have an Inventory
    > class, that obviously holds a lot of data about what is in the inventory.
    >
    > When I deal with my main program i find that I can deal with it in two
    > ways. Either
    >
    > Inventory inv = new Inventory();
    > inv.status();
    > inv.findLowStock();
    >
    > or I can acces my Inventory "directly" by:
    >
    > Inventory.status();
    > Inventory.findLowStock();
    >
    > Both seem to be working fine, but what is the difference? And more
    > important what is the best thing to do (I like good marks :))?
    >
    > Thanks,
    > /Thomas
     
    gilles, Dec 11, 2005
    #2
    1. Advertising

  3. gilles wrote:
    >
    > "status" and "findLowStock" are certainly static methods, aren't they ?


    Yes, they are decalred static.

    > Static methods or data are shared by all instances of a same class.
    > Instantiated objects will have their own versions of other data and methods,
    > but only a single copy of the ones defined as static.


    Ahh. So if I had an Inventory for e.g. several departments I'd have to
    use create an instance for each Inventory, but when I only have one
    inventory and one shop, then it doesn't matter?

    Right?
    /Thomas
     
    Thomas Korsgaard, Dec 11, 2005
    #3
  4. Thomas Korsgaard wrote:
    > gilles wrote:
    >
    >>
    >> "status" and "findLowStock" are certainly static methods, aren't they ?

    >
    >
    > Yes, they are decalred static.
    >
    >> Static methods or data are shared by all instances of a same class.
    >> Instantiated objects will have their own versions of other data and
    >> methods,
    >> but only a single copy of the ones defined as static.

    >
    >
    > Ahh. So if I had an Inventory for e.g. several departments I'd have to
    > use create an instance for each Inventory, but when I only have one
    > inventory and one shop, then it doesn't matter?
    >
    > Right?
    > /Thomas


    Short answer is yes, if you have only one inventory and one shop it
    doesn't matter.
    *BUT* if you want to do things properly, it does matter.

    Think about real world: we have for example the class "human being" that
    is the category that contains all the people in the world.
    Keep in mind that the class is a *category*, not something "you can touch".

    Now a static method or variable, is something that refers to the
    category, not to one particular object in the category.
    In this example, we could have

    static final int NUMBER_OF_EYES = 2;

    it's static, it's a class variable because all human beings have 2 eyes.

    On the other hand, if you are referring to an action or a property that
    is about one (or some) instances of the category, then it's not static
    but an instance method/field.
    For example:

    String colorOfHairs = "red";

    Because someone has red hairs, but this is different for each people.

    To get back to your case, you should create an instance of your
    shop/inventory and call non-static methods, even if you have only one of
    them.

    You should always think about real world and ask yourself: "is this
    something about the category or about one particular object in the
    category?"

    This is probably the single most important thing to understand in
    object-oriented programming.
    I suggest you take ten minutes to do a simple exercise: you write down
    on a piece of paper some classes (for ex. "vehicle", "building", etc.)
    then you try to identify five instance variables/methods and five static
    variables/methods for each one.

    I hope my explanation was not too intricated :)

    Francesco
     
    Francesco Devittori, Dec 11, 2005
    #4
  5. Thomas Korsgaard

    Chris Smith Guest

    Thomas Korsgaard <> wrote:
    > gilles wrote:
    > >
    > > "status" and "findLowStock" are certainly static methods,
    > > aren't they ?

    >
    > Yes, they are decalred static.


    You should realize that other people in this thread seem to have moved
    on and started talking about whether these methods should be static or
    not. That's a good question, but it's not exactly what you asked.

    Since the methods are static, you should NOT create an object of class
    Inventory to call them. Doing so is very confusing. The object won't
    be used at all, and you'll just be calling static methods, but lying to
    people reading your code by pretending to call non-static methods. It
    was a mistake by Sun in the language specification to allow you to to do
    so. Several popular Java development environments will give warnings
    from the compiler if you do it.

    So if you want to take Francesco's advice, then change the methods (and
    data they operate on) so they aren't static and THEN create the object
    and call the functions. But if you leave the methods static, you should
    call them like they are static methods... that is, "Inventory.status()"
    and so on.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 11, 2005
    #5
  6. Thomas Korsgaard

    zero Guest

    Chris Smith <> wrote in
    news::

    >
    > Since the methods are static, you should NOT create an object of class
    > Inventory to call them. Doing so is very confusing. The object won't
    > be used at all, and you'll just be calling static methods, but lying to
    > people reading your code by pretending to call non-static methods. It
    > was a mistake by Sun in the language specification to allow you to to do
    > so. Several popular Java development environments will give warnings
    > from the compiler if you do it.
    >
    > So if you want to take Francesco's advice, then change the methods (and
    > data they operate on) so they aren't static and THEN create the object
    > and call the functions. But if you leave the methods static, you should
    > call them like they are static methods... that is, "Inventory.status()"
    > and so on.
    >


    Your professor might ask why those methods are static though - and if you
    want good marks you'd better have a good explanation.

    In this case I think the best option is probably to indeed change the
    methods so that they're not static.

    --
    Beware the False Authority Syndrome
     
    zero, Dec 11, 2005
    #6
    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. Phil S.

    Styles missing after upgrade

    Phil S., Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    331
    Phil S.
    Jul 8, 2003
  2. Chris Mantoulidis

    (Programming) Various Syntax styles

    Chris Mantoulidis, Dec 11, 2003, in forum: C++
    Replies:
    18
    Views:
    690
    Nils Petter Vaskinn
    Dec 15, 2003
  3. Amali

    programming styles

    Amali, Feb 5, 2007, in forum: Java
    Replies:
    16
    Views:
    492
    MikeNereson
    Feb 7, 2007
  4. Replies:
    3
    Views:
    273
    Michael Hoffman
    May 20, 2007
  5. Bill
    Replies:
    16
    Views:
    244
    David Ross
    Oct 17, 2004
Loading...

Share This Page