Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Styles of programming

Thread view: 
Thomas Korsgaard - 11 Dec 2005 10:08 GMT
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 - 11 Dec 2005 10:25 GMT
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

> Hi,
>
[quoted text clipped - 19 lines]
> Thanks,
> /Thomas
Thomas Korsgaard - 11 Dec 2005 10:29 GMT
> "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
Francesco Devittori - 11 Dec 2005 14:35 GMT
>> "status" and "findLowStock" are certainly static methods, aren't they ?
>
[quoted text clipped - 11 lines]
> 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
Chris Smith - 11 Dec 2005 17:15 GMT
> > "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.

Signature

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

zero - 11 Dec 2005 23:56 GMT
> 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
[quoted text clipped - 9 lines]
> 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.

Signature

Beware the False Authority Syndrome



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.