-3
static public void doRestrictionList(HttpServletRequest request, pageBean UTIL)

Can we declare a method as final like.

John
  • 1,191
  • 3
  • 19
  • 29
  • 3
    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

7 Answers7

4

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();
Michael McGowan
  • 6,528
  • 8
  • 42
  • 70
3

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.

Chris Kooken
  • 32,730
  • 15
  • 85
  • 123
3

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)

Community
  • 1
  • 1
wkl
  • 77,184
  • 16
  • 165
  • 176
3

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()
TheJediCowboy
  • 8,924
  • 28
  • 136
  • 208
1

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(...);
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
1

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

kukudas
  • 4,834
  • 5
  • 44
  • 65
1

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

kasgoku
  • 5,057
  • 5
  • 20
  • 20