27 Replies - 1888 Views - Last Post: 27 August 2010 - 01:04 PM
#1
Static variables
Posted 23 August 2010 - 06:56 AM
I hope I got the forum right, it's more of Java help than an actual discussion.
Replies To: Static variables
#2
Re: Static variables
Posted 23 August 2010 - 07:05 AM
Another time to use static is if there will be some sort of variable that is the same in all instances, like some sort of calculations. This way, you only have one static variable used by all classes instead of many instance variables with the same value.
If you need a counter for each instance of the class, such as an internal counter, it's better to use a non-static counter so that it is unique to that instance of the class. There aren't downsides to using static variable, only wrong situations
#3
Re: Static variables
Posted 23 August 2010 - 07:07 AM
This post has been edited by macosxnerd101: 23 August 2010 - 07:40 AM
Reason for edit:: Thanks Mercurial for catching the typo!
#4
Re: Static variables
Posted 23 August 2010 - 07:15 AM
If you're looking to do globals, don't. If you must do globals, look into the Singleton Design Pattern.
Beyond an object counter, as you mentioned, constants, and the required main entry point, static is usually just a crutch. Even a counter is better done through some kind of factory pattern.
#5
Re: Static variables
Posted 23 August 2010 - 07:20 AM
Quote
There is a design pattern called singleton, which ensures you can only ever create one instance of a particular class. One of its beauties is that it's easy to change your mind and edit a couple of lines to make multiple instances possible. I've lost count of the number of times I've had to do that. Between an initial poor design and changing requirements, you never really know which code is bulletproof and which code is going to have to be redone again and again. It's best to program defensively to make changing your code less of a headache.
If you have constants (that is values which apply to every instance of the class and can never be changed) then static variables are a good choice. Otherwise there are very few times where a static variable is a good thing. If you think it makes your life easier then you are probably not using an object orientated design. To this end, perhaps you could provide an example which you consider easier as a static variable, and if there is a more OO way of doing it, I'll post my suggestion.
Edit: I believe I have just been ninjaed.
This post has been edited by cfoley: 23 August 2010 - 07:21 AM
#6
Re: Static variables
Posted 23 August 2010 - 07:21 AM
#7
Re: Static variables
Posted 23 August 2010 - 07:39 AM
#8
Re: Static variables
Posted 23 August 2010 - 07:41 AM
#9
Re: Static variables
Posted 23 August 2010 - 07:55 AM
macosxnerd101, on 23 August 2010 - 06:41 AM, said:
Just to make sure someone else doesn't get it wrong.
cfoley, on 23 August 2010 - 06:20 AM, said:
I am, but I still consider myself a Java beginner (started in Feb). I've learned about OOP concepts, and sometimes I find it hard to implement them since I come from a C background. I realized I can make most things work (like I did in C), but the point is to stick to OOP concepts. So when I wouldn't know how to implement something, I'd make it static just to make it work so I can focus on other stuff and deal with it later (it makes a big mess outta your code, I'm tellin' ya). Nowdays I try to do it the right way, which can be a problem mostly because of my lack of experience.
In other words, using static to make it work is a no-no. Well, define 'make it work'...
#10
Re: Static variables
Posted 23 August 2010 - 08:00 AM
In other words, get it working vs. get it working the right way.
If you aren't getting the OO concepts, you should read up on them with some of the tutorials in the Java and Software Development Tutorials sections.
#11
Re: Static variables
Posted 23 August 2010 - 08:26 AM
Mercurial, on 23 August 2010 - 08:55 AM, said:
Using static.
Honestly, beyond the entry point, static is probably one of the most abused elements of Java. It allows procedural ( C ) programmers to fake it. I don't believe it should ever be taught to beginners, because it can rise to the level of evil goto does.
Every language has different ways of getting the job done. A competent programmer should find the most natural way to accomplish a task in a given environment, rather than dragging baggage over from some other environment.
I like C. I admire it's simplicity. I do things in C I wouldn't do anywhere else, like a static global in a source file. Because, in the context of C, that's a singleton for that object code. C pulls that kind of stuff all the time. You don't actually need header files in C projects, though your really should. I use macros and defines all over the place in C. These are things that are terrible practices in most other languages, but good for C.
Know your language. Work with it, not against it.
#12
Re: Static variables
Posted 23 August 2010 - 08:40 AM
macosxnerd101, on 23 August 2010 - 07:00 AM, said:
Perfect
baavgai, on 23 August 2010 - 07:26 AM, said:
I must admit your posts are very instructive(mostly 'from my experience' rather than 'here are the facts'), even though I didn't quite understand the bolded parts.
#13
Re: Static variables
Posted 23 August 2010 - 08:42 AM
#15
Re: Static variables
Posted 23 August 2010 - 09:36 AM
macosxnerd101, on 23 August 2010 - 09:44 AM, said:
Yep, that's the meaning of it.
Your question was, "define 'make it work'..." Make it work can also be, "just get it done, doesn't really matter if it's crooked, no one is going to see that part."
Using static is often done when the programmer can't visualize a viable OO approach. Cut and paste is used when the programmer is too damn lazy to refactor their cut and pasting into a proper method abstraction.
Craft means that programming isn't strictly assembling parts. The is no single "correct" solution for any given programming problem; the permutations of possible solutions for anything non trivial approach infinity. Rather, an experienced programmer can gage the level of knowledge, and competence, of another by looking at their code.
When you take pride in your work, you don't want to leave it unfinished, or shoddily constructed, even if you can. Even if no one else will know. That's craft.
|
|

New Topic/Question
Reply



MultiQuote








|