static public void doRestrictionList(HttpServletRequest request, pageBean UTIL)
Can we declare a method as final like.
static public void doRestrictionList(HttpServletRequest request, pageBean UTIL)
Can we declare a method as final like.
Static methods generally make sense when the functionality does not need to be attached to an actual object but the logic makes sense to attach to the class. Often this is the case for utility type methods that depend on a small number of parameters. For instance, I prefer this:
int sum = Arithmetic.sum(13, 42);
to this:
Arithmetic arithmeticObject = new Arithmetic();
int sum = arithmeticObject.add(13, 42);
or this:
Arithmetic arithmeticObject = new Arithmetic(13, 42);
int sum = arithmeticObject.add();
Static means it does not require any of the instance variables on the class to function properly. It can allow for a more robust API as you can call your method by:
MyClass.doRestrictionsList(req, util);
If you didn't make it static, you would have to instantiate your class to give it state, then call your method on that instance.
Static methods can be utilized without having to instantiate the class they belong to.
From http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html
Class Methods
The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in
ClassName.methodName(args)
Adding onto what everybody else said. A normal class method you would have to instantiate the object as follows.
If you have a class called ClassExample with a method testMethod You would have to do the following to call it
ClassExample example = new ClassExample();
example.testMethod()...
if you have a static method you do not have to worry about instantiating the object so you can do something similar to the following
ClassExample.testMethod()
We declare methods static so they can be accessed without holding an instance of an Object based on that class.
Using your example I could call
SurroundingClass.doRestrictionList(...);
Without the static key word, I need to do something like this
SurroundingClass object = new SurroundingClass();
object.doRestrictionList(...);
Static methods are used a lot for utility / convienent methods for which you want to avoid the cost of creating a new instance.
Your post title and question don't match. Are you looking for a comparison between static and final methods? Everybody has described static methods, let me describe final methods for you.
Can you have final methods in Java? Yes
Declaring a method as final means that it cannot be overridden by any of the implementing subclasses. From http://download.oracle.com/javase/tutorial/java/IandI/final.html
You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object