Wow.... um, that's a very convoluted question, to begin with.
The fact is that static methods are linked to a "class", where as instance methods and fields are linked to the specific instance, or object, of the class in which they are declared.
For example, if you have several instances of a class Foo, then each instance has a separate copy of the field T instanceFoo
. The method void instanceFooOperation()
operates on the copy of instanceFoo contained by the instance of Foo upon which instanceFooOperation was invoked.
On the other hand, if you have several instances of a class Foo, you will still only have ONE copy of the field static T staticFoo
. This class variable is accessible to ALL instances of the class, but belongs to none of them. The same is true of static operations.
For example, let's say you had the following code:
public class Person {
static String firstName;
static String lastName;
}
static public void main(String [] args) {
Person paul = new Person();
paul.firstName = "Paul"; paul.lastName = "McCartney";
Person john = new Person();
john.firstName = "John"; john.lastName = "Lennon";
Person ringo = new Person();
ringo.firstName = "Ringo"; ringo.lastName = "Starr";
Person george = new Person();
george.firstName = "George"; george.lastName = "Harrison";
Sys tem.out.println(paul.firstName);
}
The output is not, as expected, Paul, but rather, is George, because John, Paul, George, and Ringo all have the same first name, and it must always be so, because we have a static variable.
Static variables CAN be useful, but they cannot replace instance variables, otherwise, there would be no point in having instances, because what makes to instances different is the values in their instance variables.
As for the pros and cons, once you understand what a static variable is, you should be able to figure it all out for yourself.
- Adam
> Seems pretty clear to me: "Why instantiate a class
> when you can make the methods static?"
To me, that pretty much equates to "Why hire 100 developers, when you can just hire 1?"
Answer: You COULD hire just one, but you can do a hell of a lot more with 100, and there are things that you just can't do, at least, in any realistic manner, with only 1.
- Adam
> >>Seems pretty clear to me: "Why instantiate a class
> when you can make the methods static?"
>
> Yes thats the write question, Sorry I didn't asked it
> properly.
Because just writing static methods violates the whole point of using object-oriented programming, which is encapsulation.
Take a step back and look at computer programming, or better yet the evolution of computer programming, as a whole. A computer program tells the computer what to do, down to excrutiating detail (look at assembly language). You can't tell "create a 3d world and put monsters in it and let me fire guns at them"; you (or some other human) has to break all those things down into progressively simpler actions until it comes down to moving numbers around. Trillions of numbers.
You end up with a lot of detailed information, and that gets complex. So computer programming is really about creating complexity, and then managing that complexity. Each subsequent step in the development of computer languages has been about managing that complexity -- it's simpler to write machine code in files rather than flipping switches on the console; it's simpler to write assembly language than machine code; it's simpler to compile a higher-level language than write assembly language, etc. Each next advance is a way to manage the complexity of computer programs, often by delegating the detail work to lower levels.
Object-oriented programming seeks to manage complexity by using encapsulation. This means that data is closely tied to the code (the methods) that manipulate the data, and the data is hidden from everything (as much as possible anyway) other than the methods. Thus you can deal with complexity in clear, separate units: you're either working within an object, or you're combining objects, working from the outside.
If you use static methods (or for that matter, the Singleton pattern) too much, then you're basically throwing that encapsulation away; the data becomes exposed to everything else. And thus you're throwing away the whole point of using an OO language, and you're creating ugly hairy difficult-to-maintain code.