View Full Version : static methods

07-30-2009, 05:54 AM
in java a static method can access only static data members but main() is a static method
still it is accessing the instance data members by creating an object of other class.
how can it be possible?"

****** report (http://www.adverse-mortgage-centre.co.uk/******-report.html)
pay advance (http://www.supercash*2*.com)

08-05-2009, 03:14 AM
You have to be a little more specific than that.
What kind of data types are you using?
Why do you have to use static methods?
In theory, using static methods in a single class is just like using non-static methods, only you have one instance of them in the whole class.

08-20-2009, 05:03 AM
Recently many of you, after reading Guide to Testability, wrote to telling me there is nothing wrong with static methods. After all what can be easier to test than Math.abs()! And Math.abs() is static method! If abs() was on instance method, one would have to instantiate the object first, and that may prove to be a problem. (See how to think about the new operator, and class does real work)

The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I wire the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is nothing to “wire&#822*; since there are no objects, the code and data are separate.

09-10-2009, 03:54 AM
The Java construct for implementing functions is known as the static method. In this section, you will learn how to organize your programs by defining and using other static methods within them.

Using and defining static methods.
As you know from using Java's Math library, the use of static methods is easy to understand. For example, when you write Math.abs(a-b) in a program, the effect is as if you were to replace that code by the value that is computed by Java's Math.abs() method when presented with the value a-b. This usage is so intuitive that we have hardly needed to comment on it. If you think about what the computer has to do to create this effect, you will realize that it involves changing a program's flow of control.

You can define static methods other than main() in a .java file, as illustrated in Newton.java. This implementation is better than our original implementation because it clearly separates the two primary tasks performed by the program: calculating the square root and interacting with the user. We examine it in detail so that you can think carefully about what a static method is and how it operates.
tøj (http://www.smartguy.dk/)
maternity nursing bras (http://www.laurensilva.com)