Styles of programming

T

Thomas Korsgaard

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
 
G

gilles

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
 
T

Thomas Korsgaard

gilles said:
"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
 
F

Francesco Devittori

Thomas said:
Yes, they are decalred 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
 
C

Chris Smith

Thomas Korsgaard said:
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
 
Z

zero

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.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top