Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Styles of programming

Reply
Thread Tools

Styles of programming

 
 
Thomas Korsgaard
Guest
Posts: n/a
 
      12-11-2005
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
 
Reply With Quote
 
 
 
 
gilles
Guest
Posts: n/a
 
      12-11-2005
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


 
Reply With Quote
 
 
 
 
Thomas Korsgaard
Guest
Posts: n/a
 
      12-11-2005
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
 
Reply With Quote
 
Francesco Devittori
Guest
Posts: n/a
 
      12-11-2005
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
 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      12-11-2005
Thomas Korsgaard <(E-Mail Removed)> 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
 
Reply With Quote
 
zero
Guest
Posts: n/a
 
      12-11-2005
Chris Smith <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about programming styles fdu.xiaojf@gmail.com Python 3 05-20-2007 10:46 AM
programming styles Amali Java 16 02-07-2007 01:14 PM
Ruby programming styles and new program? Bill Ruby 16 10-17-2004 12:35 AM
(Programming) Various Syntax styles Chris Mantoulidis C++ 18 12-15-2003 09:09 AM
Column Styles Rich MCSD 0 10-02-2003 11:29 PM



Advertisments