2

There were no takers on the java room, so let me ask it here.

OOP is an object-based approach to coding, and some mapping can be done, such as saying that a mug can be represented as a Mug (Mug mug = new Mug()), and that Mug implements the Interface Grabbable. But if we follow this line of thought, what would static fields/methods be? Or is static something that cannot be mapped?

3
  • Normally people tend to relate static methods as something for multiple\all mugs, kinda like utility methods, and instance methods are used to change the state of that particular Mug. Commented Jul 3, 2014 at 2:54
  • 1
    I imagine a bunch of mugs hanging on a single, horizontal metal pole, by their handles, above a bar. The bartender slides the one closest to the end off, fills it, and gives it to a customer. The pole is static. There's only one pole, and its used by all mugs. Commented Jul 3, 2014 at 2:58
  • 4
    Don't get too excited about this kind of analogy. In the real world, OOP objects are not used to represent real-world objects: they are used to represent programming constructs, such as connection, statement, collection, socket, channel, buffer, list, thread, process, stream, ... The reason is that customers can also be vendors, employees can become managers and vice versa, ordinary customers can become wealth customers and vice versa, ... Commented Jul 3, 2014 at 3:10

3 Answers 3

1

In your analogy, a static method or field is something that is grouped with all Mugs but doesn't require a particular (or any) Mug to be used.

public class Mug {
  public static void doStatic() {}
  public void drink() {}
}

To drink() you need an instance of Mug, but you can call Mug.doStatic() without one. However, doStatic() cannot access this (because it isn't associated with an instance).

Edit

A static type of operation might be a factory or builder (for the creation of Mugs), it might be counting some property of Collections of Mugs, or some operation entirely unrelated to Mugs like a main method.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this is how static is defined in Java, but not a description of what could an object in real life be mapped to a static field. Would you have another example, of a real life action, attribute, or property that would be an equivalent in real life to what static is in OOP?
That would make more sense. For the Mugs to know how many Mugs were created. Given that Mugs can be created in different factories, it would make sense that the factory is aware of how many mugs it created but not how many mugs in general were created.
1

A static field would be a property shared by all mugs. For example numberOfMugsInTheCloset.

Non final static fields are often a bad practice though...

3 Comments

Wouldn't that be a property of the Closet Collection?
also, I don't see why any Mug would need to be aware of the closet or how many mugs are in a particular closet.
@AlexandreSantos I totally agree, really bad design. Right now I have no example of a static field which would make sense in a well designed app...
0

To keep the same line of thought, consider this from real world object Mug. Mug - Green Mug - Red Mug - Blue. Considering that Shape and Size of the all three Mug is same than color would be variant property. Now consider another property of the same Mug - CAPACITY. No matter what the color of the Mug CAPACITY is same (static property of the Mug). This way we can map non-variant property (static) to Entity.

4 Comments

Wouldn't CAPACITY be defined in the interface for Mug? public class Mug implements Grabbable, Capacity?
No, I don't see any reason why CAPACITY be defined in Grabbable. It will be defined in MUG.
Would you say that all mugs have the same capacity?
Yes, I have mentioned the same in the answer " Considering that Shape and Size of all Mugs is same".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.