1. When we know for sure that the results from the method does n't change across with instances, but with the parameter values that are passed into.
2. For a Car class, getColor is non static, where as convertMilesToKilo should be static.
3. If we know for sure that the methods are not going to change.
4. If it makes sense to call the method even before the Instances are created.
why you wouldn't want to create a static method? Basically, polymorphism goes out of the window. You'll not be able to override the method. It takes a lot of flexibility out from your design. Also, if you need state, you'll end up with lots of concurrency bugs and/or bottlenecks if you are not careful.
Static doesn't quite mean "shared by all instances" - it means "not related to a particular instance at all". In other words, you could get at the static field in class A without ever creating any instances.
As for running two programs within the same JVM - it really depends on exactly what you mean by "running two programs". The static field is effectively associated with the class object, which is in turn associated with a classloader. So if these two programs use separate classloader instances, you'll have two independent static variables. If they both use the same classloader, then there'll only be one so they'll see each other's changes.
For example,
private static Long ONE_HOUR_IN_MILLIS = 1000*60*60;
The field ONE_HOUR_IN_MILLIS is a Class variable and not an instance variable, the value it represents does n't change with instances
Static Classes - There is not a top level class that is static in Java, but we can have static nested classes
By following we can convert a normal class to static
1. Make the class as Final so it cannot be subclassed. Since extending a static class makes no sense, we extend a class to acquire properties or
2. Make the constructor private so that others cannot create instances of the class
3. Make all members and functions as static since static class cannot be instantiated there is no point in making instance methods and variables.
Possible uses of a static class
A good use of a static class is in defining one-off, utility and/or library classes where instantiation would not make sense. A great example is the Math class that contains some mathematical constants such as PI and E and simply provides mathematical calculations. Requiring instantiation in such a case would be unnecessary and confusing. See Java's Math class. Notice that it is final and all of its members are static. If Java allowed top-level classes to be declared static then the Math class would indeed be static.
2. Make the constructor private so that others cannot create instances of the class
3. Make all members and functions as static since static class cannot be instantiated there is no point in making instance methods and variables.
Possible uses of a static class
A good use of a static class is in defining one-off, utility and/or library classes where instantiation would not make sense. A great example is the Math class that contains some mathematical constants such as PI and E and simply provides mathematical calculations. Requiring instantiation in such a case would be unnecessary and confusing. See Java's Math class. Notice that it is final and all of its members are static. If Java allowed top-level classes to be declared static then the Math class would indeed be static.
The only difference with inherited static (class) methods and inherited non-static (instance) methods is that when you write a new static method with the same signature, the old static method is just hidden, not overridden.
Usage of Inheritance
1. Code re-use
2. A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members
3. Polymorphism
Source and Info : http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods
No comments:
Post a Comment