• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static V Non-static  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I was wondering which is best to use.
I have a chess program that needs to use prestored arrays.
I currently have these arrays in a separate class, with all of them static.

Instead, I could make them non-static, and instantiate the class, thus calling them through the object name instead of class name.

I'm basically looking for the fastest solution since these arrays may be needed a million times a second, so should I switch to non-static.

I read somewhere non-static is better, hence this topic. Any ideas?

Thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding performance, I doubt that it makes much of a difference. There are other things you will have to a million times a second which take much longer than accessing a field.

The problem with static fields is that they are basically globals - with all the typical problems they are associated with.

So, yes, I would make them instance fields, but not because of performance reasons, but because of maintainability reasons. And I'd look out for putting actual behaviour into the class - remember, the reason to use an OO language is to use its tools for dependency management. And one of the most important tools is encapsulation - putting the data and the operation of the data together, and only publicize the operations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!