Take the 2-minute tour ×
static public void doRestrictionList(HttpServletRequest request, pageBean UTIL)

Can we declare a method as final like.

share|improve this question
Can you please clarify your question? The heading contains static, the description contains an unfinished sentence referring to final. What do you want to know? –  Felix Kling Oct 18 '10 at 21:26
add comment

closed as not a real question by Colin Hebert, Andrey, erickson, Simone Carletti, bmargulies Oct 20 '10 at 0:07


7 Answers

up vote 2 down vote accepted

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();

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

share|improve this answer
whats the use of having a method declaring as final –  John Oct 18 '10 at 21:39
It stops any subclass from overriding that method, which can help for security in your class. –  Christian Mann Oct 18 '10 at 21:49
exactly as Christian has said, but in lesser terms, it makes the method immutable –  CitadelCSAlum Oct 18 '10 at 22:58
add comment

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();
share|improve this answer
+1 for showing more than only consequences of using or not using static –  Carlos Heuberger Oct 18 '10 at 21:44
Or this if you like fancy code: int sum = (new Arithmetic()).add(13, 42); –  Christian Mann Oct 18 '10 at 21:51
add comment

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.

share|improve this answer
add comment

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


share|improve this answer
add comment

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


Without the static key word, I need to do something like this

SurroundingClass object = new SurroundingClass();
share|improve this answer
add comment

Static methods are used a lot for utility / convienent methods for which you want to avoid the cost of creating a new instance.

share|improve this answer
add comment

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

share|improve this answer
add comment
