RE: [Java] static and non-static 07-17-2013, 08:56 PM
#11
(07-17-2013, 05:38 PM)JDKlett Wrote: Please do not forget that using the static variables could push to a bad designed applications. Use it only if you do not have any other choice, at least for simple applications. For example the information hiding principle becomes hard to be managed!
You shall not avoid static at all cost, you shall use it when it is applicable. That has nothing to do with having small or big applications. If you have a small application you should still create code that is reuseable, thus a class you can just take as is for another application.
An application that has non-static methods that aren't instance specific is as well bad designed.
An example is the factory pattern:
Code:
public class CarFactory {
private static int numberOfCars = 0;
public static Car createCar() {
numberOfCars++;
return new Car();
}
public static int getNumberOfCars() {
return numberOfCars;
}
}None of these should be non-static.
Some examples of the Java standard library are:
Code:
Java: java.util.Collections.singletonMap()
Java: javax.xml.parsers.DocumentBuilderFactory.newInstance()I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.
Expressed feelings are just an attempt to simulate humans.
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)


![[+]](https://sinister.li/images/modern/collapse_collapsed.png)

