Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > applicability of static methods in web applications?

Reply
Thread Tools

applicability of static methods in web applications?

 
 
vishist
Guest
Posts: n/a
 
      04-05-2007

Hi,
For my project, I see that some people use static methods for the
web-application in DB layer.

eg:

public static List getId(){
return list;
}

Now, I know that this method works in a threaded environment since all
these methods fall in thread scope. But coding like this defeats the
whole purpose of choosing a object oriented language. I'm confused
here whether it is a good style or not.

You comments are really appreciated..

V.

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      04-06-2007
On Apr 5, 1:34 pm, "vishist" <(E-Mail Removed)> wrote:
> Hi,
> For my project, I see that some people use static methods for the
> web-application in DB layer.
>
> eg:
>
> public static List getId(){
> return list;
>
> }
>
> Now, I know that this method works in a threaded environment since all
> these methods fall in thread scope. But coding like this defeats the
> whole purpose of choosing a object oriented language. I'm confused
> here whether it is a good style or not.
>
> You comments are really appreciated..
>
> V.


I personally think its quite bad style. There are, however, some
limitations with standard servlets, it is often easier to have static
utility methods.

I've actually gone as far personally to create a wrapper object when
contains a request, and all sorts of other information, and have that
object handle the business logic. Only in a couple Tag classes (where
its unavoidable) do I access anything static or at an application
scope. and in those cases its usually to access a factory object
which has been configured through Spring (IoC is the way to be)

 
Reply With Quote
 
 
 
 
vishist
Guest
Posts: n/a
 
      04-06-2007
On Apr 5, 8:02 pm, "Daniel Pitts" <(E-Mail Removed)> wrote:
> On Apr 5, 1:34 pm, "vishist" <(E-Mail Removed)> wrote:
>
>
>
> > Hi,
> > For my project, I see that some people use static methods for the
> > web-application in DB layer.

>
> > eg:

>
> > public static List getId(){
> > return list;

>
> > }

>
> > Now, I know that this method works in a threaded environment since all
> > these methods fall in thread scope. But coding like this defeats the
> > whole purpose of choosing a object oriented language. I'm confused
> > here whether it is a good style or not.

>
> > You comments are really appreciated..

>
> > V.

>
> I personally think its quite bad style. There are, however, some
> limitations with standard servlets, it is often easier to have static
> utility methods.
>
> I've actually gone as far personally to create a wrapper object when
> contains a request, and all sorts of other information, and have that
> object handle the business logic. Only in a couple Tag classes (where
> its unavoidable) do I access anything static or at an application
> scope. and in those cases its usually to access a factory object
> which has been configured through Spring (IoC is the way to be)


Hi,
Thanks for responding to my request.
When you say personally, isn't there any steadfast rule that says you
shouldn't create static methods that has business logic in them?.
Also, when you say limitations with standard servlets, can you please
elaborate on them? I mean I want to understand your perspective.
Let me rephrase my question:
In a multi-threaded object oriented programming, is it wise to have
static methods with business logic embedded in them?


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-06-2007
vishist wrote:

> Let me rephrase my question:
> In a multi-threaded object oriented programming, is it wise to have
> static methods with business logic embedded in them?


I think that can be answered quite simply: no.

Static methods in general have many "bad" features: they introduce excessive
coupling, they make the system inflexible, they are hostile to certain testing
methodologies. Perhaps more importantly they have nothing whatever to do with
OO programming (and if you don't think that OO programming is an advantage for
the application you are writing then you are wasting your time using Java at
all).

That's not to say that you should avoid static methods entirely -- they have
their place in any reasonable design. But you should regard each static method
with suspicion, and ask yourself "what am I /really/ trying to do here ?"
Static methods are useful in implementing various kinds of glue (to allow one
part of a system to find another part of a system), but they are never the
/only/ way of implementing that glue, and sometimes they are not the /best/ way
of doing it. Static methods can also be useful for helper/utility methods of
various kinds (consider the static methods on java.util.Arrays, for instance),
but again they are not necessarily the /best/ way of packaging utility code. I
don't usually like absolutes in programming or design ("never do XYZ" or
"always do XYZ") but on this subject I feel safe in sticking my neck out and
stating that "real" logic should never be implemented in static methods.

(All the above applies regardless of whether you are working in a Servlet-style
context).

-- chris


 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      04-06-2007
Chris Uppal wrote:
>
> Static methods in general have many "bad" features: they introduce excessive
> coupling, they make the system inflexible, they are hostile to certain testing
> methodologies. Perhaps more importantly they have nothing whatever to do with
> OO programming (and if you don't think that OO programming is an advantage for
> the application you are writing then you are wasting your time using Java at
> all).


I would be very wary about making a method non-static just on the basis
of principle. You[1] may be coupling the method to a particular object
for no specific purpose. When you do find that the method just become an
instance method, then there is no reason to suspect that you put it on
the right object. So then it's a case of disentangling the method before
you can re-entangle it.

Therefore, I suggest prefer static methods "all other things being
equal" (which they probably wont be).

But not static variables. Static variables are evil.

Tom Hawtin

[1] My usual "indefinite you".
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      04-07-2007
vishist wrote:
> For my project, I see that some people use static methods for the
> web-application in DB layer.
>
> eg:
>
> public static List getId(){
> return list;
> }
>
> Now, I know that this method works in a threaded environment since all
> these methods fall in thread scope.


That code looks very thread unsafe to me.

Arne
 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      04-07-2007
Arne Vajh°j wrote:
> vishist wrote:
>>
>> public static List getId(){
>> return list;
>> }
>>
>> Now, I know that this method works in a threaded environment since all
>> these methods fall in thread scope.

>
> That code looks very thread unsafe to me.


You can't tell from a fragment.

private static final List list =
Collections.unmodifiableList(Arrays.asList(new String[] {
"A", "B", "C"
}));

private static final List list = new Vector();

Tom Hawtin
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      04-07-2007
Tom Hawtin wrote:
> Arne Vajh°j wrote:
>> vishist wrote:
>>>
>>> public static List getId(){
>>> return list;
>>> }
>>>
>>> Now, I know that this method works in a threaded environment since all
>>> these methods fall in thread scope.

>>
>> That code looks very thread unsafe to me.

>
> You can't tell from a fragment.
>
> private static final List list =
> Collections.unmodifiableList(Arrays.asList(new String[] {
> "A", "B", "C"
> }));
>
> private static final List list = new Vector();


The original post stated that the code was in DB layer.

A static method returning a static field in a DB layer
does look thread unsafe.

It is possible to create absurd examples where it is
not, but I do not see much point in that.

Arne
 
Reply With Quote
 
vishist
Guest
Posts: n/a
 
      04-07-2007
Arne Vajh°j wrote:
> Tom Hawtin wrote:
>> Arne Vajh°j wrote:
>>> vishist wrote:
>>>>
>>>> public static List getId(){
>>>> return list;
>>>> }
>>>>
>>>> Now, I know that this method works in a threaded environment since all
>>>> these methods fall in thread scope.
>>>
>>> That code looks very thread unsafe to me.

>>
>> You can't tell from a fragment.
>>
>> private static final List list =
>> Collections.unmodifiableList(Arrays.asList(new String[] {
>> "A", "B", "C"
>> }));
>>
>> private static final List list = new Vector();

>
> The original post stated that the code was in DB layer.
>
> A static method returning a static field in a DB layer
> does look thread unsafe.
>
> It is possible to create absurd examples where it is
> not, but I do not see much point in that.
>
> Arne


I've been following newgroups recently and never knew that there will be
responses to my question after couple of days. I'm sorry.

Now, to Arne's comment,
Things are working at our end (though it might be absurd). The variables
declared in static method fall under thread scope right?. The DB
Connection, statement, resultset all fall in the same static method.

public static List getMyResultSet(){
Connection conn = getConnection();
PreparedStatement pStatement = conn.prepareStatement();
ResultSet rSet = pStatement.executeQuery();
List resultSetObjectList = new ArrayList();
...................
...................//Populating resultSetObjectList here
...................
return resultSetObjectList;
}

this is how our code is written and is working now. We invoke these
methods statically from the servlet prefixing the Class name. I know
that this is not a good style. Infact that was my question:
----isn't there any steadfast rule that says you shouldn't create static
methods that has business logic in them?.

Please comment!

vishist.
 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      04-07-2007
vishist wrote:
> Things are working at our end (though it might be absurd). The variables
> declared in static method fall under thread scope right?. The DB
> Connection, statement, resultset all fall in the same static method.
>
> public static List getMyResultSet(){
> Connection conn = getConnection();
> PreparedStatement pStatement = conn.prepareStatement();
> ResultSet rSet = pStatement.executeQuery();
> List resultSetObjectList = new ArrayList();
> ...................
> ...................//Populating resultSetObjectList here
> ...................
> return resultSetObjectList;
> }
>
> this is how our code is written and is working now. We invoke these
> methods statically from the servlet prefixing the Class name. I know
> that this is not a good style. Infact that was my question:
> ----isn't there any steadfast rule that says you shouldn't create static
> methods that has business logic in them?.


There are no problems with that code.

The code you posted was:

public static List getId(){
return list;
}

which looked as if list was a static field.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clarification on the applicability of compile-time optimization to astruct variable which encompasses a mixture of fields declared as volatileand non-volatile individually Myth__Buster C Programming 45 07-15-2012 04:46 AM
Applicability of exams introduced since Certified Partner competencyrequirements set Ion Freeman Microsoft Certification 2 05-13-2009 08:15 PM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Applicability KB 831138: app that uses WebRequest has OutOfMemoryE =?Utf-8?B?dmVjb3pvQG9ubGluZS5ub3NwYW0=?= ASP .Net 9 01-05-2006 08:50 PM
Why Petshop Changed all static methods to instance methods when upgrading from version 3.0 to version 3.1? Neo ASP .Net 1 01-07-2005 01:46 AM



Advertisments