Are there any advantages to using static methods?

A

Adam Lipscombe

Folks,


I have JBuilder 2005 and the code audit functionality continually warns me
that instance methods can be made static when they do not reference a
instance variable.

Fair enough, these methods *could* be made static, but is there any
advantage, speed or otherwise to doing this?


TIA - Adam
 
A

Andrea Desole

Adam said:
Folks,


I have JBuilder 2005 and the code audit functionality continually warns me
that instance methods can be made static when they do not reference a
instance variable.

Fair enough, these methods *could* be made static, but is there any
advantage, speed or otherwise to doing this?

you can call it without building an object
 
A

Adam Lipscombe

In this case the methods are private so they could not be called directly.
The code audit complains no matter what the scope of the method, so I can
only thinkl therte must be another reason why they should be static.


Adam
 
M

Malte

Adam Lipscombe wrote:

Oracle JDeveloper Audit (the new beta) insists on suggesting that this:

String test = null;

be replaced by this:

String test;
test = null;

Some of these tools are peculiar..
 
A

Andrea Desole

Adam said:
In this case the methods are private so they could not be called directly.
The code audit complains no matter what the scope of the method, so I can
only thinkl therte must be another reason why they should be static.

I personally can't think of any other advantage. I don't think there is
much difference between a call to a static member and a call to an
instance member.
I guess JBuilder does that without checking if the method is private or
not. It can also be possible that, if the private method a uses only
static methods/members, it can be called by another public method b;
therefore, if you make a static, you might make b static too.
 
K

Karel Suikers

Adam Lipscombe schreef:
I have JBuilder 2005 and the code audit functionality continually warns me
that instance methods can be made static when they do not reference a
instance variable.

Fair enough, these methods *could* be made static, but is there any
advantage, speed or otherwise to doing this?

If these methods don't use any variables of that class, why are these
methods in that class?
There is a big change that these methods belong to another class.

Karel
 
C

Chris Uppal

Adam said:
I have JBuilder 2005 and the code audit functionality continually warns me
that instance methods can be made static when they do not reference a
instance variable.

Fair enough, these methods *could* be made static, but is there any
advantage, speed or otherwise to doing this?

No.

If -- because of the way you have chosen to structure your application -- they
/have/ to be static, then you'll already know it (and they'll already /be/
static ;-).

Otherwise, the advice from the audit is Just Plain Wrong. Ignore it.

-- chris
 
B

Boudewijn Dijkstra

Adam Lipscombe said:
Folks,


I have JBuilder 2005 and the code audit functionality continually warns me
that instance methods can be made static when they do not reference a
instance variable.

Fair enough, these methods *could* be made static, but is there any
advantage, speed or otherwise to doing this?

A non-static method implies a dependance on an instance of the underlying
class. If method body analysis shows that it is in fact not dependant, then
there is no point in having it non-static.

Also, when passing arguments to methods, a 'this' pointer is passed along when
the method is non-static. This gives a slight advantage to static methods.
 
J

John C. Bollinger

Boudewijn said:
A non-static method implies a dependance on an instance of the underlying
class. If method body analysis shows that it is in fact not dependant, then
there is no point in having it non-static.

That's painting with a bit too broad a brush. Consider an
implementation of the Strategy pattern where some or all of the strategy
objects are independent of instance state. They still need their
perform() methods to be instance methods because the instance functions
as a method selector.

More generally, anywhere you want polymorphic behavior -- including
implementation methods of an interface -- you must use instance methods,
whether or not the methods have any dependence on instance state.
Method body analysis does not take this consideration into account.
Also, when passing arguments to methods, a 'this' pointer is passed along when
the method is non-static. This gives a slight advantage to static methods.

Well yes, I suppose so, but using a static method instead of an instance
method for this reason is pretty far down on the list of fruitful
optimizations. It is certainly not sufficient to overcome _any_
indication in favor of an instance method. The appropriate choice is
not always clear-cut.
 
T

Tom McGlynn

John said:
...


More generally, anywhere you want polymorphic behavior -- including
implementation methods of an interface -- you must use instance methods,
whether or not the methods have any dependence on instance state. Method
body analysis does not take this consideration into account.

I think the OP indicated that the methods involved were private.
Presumably it would be a bug in the analysis tool, not just an
annoying feature, it it was spewing out the messages that aroused his ire
for overrideable methods.

Regards,
Tom McGlynn
 
H

HK

Adam said:
I have JBuilder 2005 and the code audit functionality continually warns me
that instance methods can be made static when they do not reference a
instance variable.

Fair enough, these methods *could* be made static, but is there any
advantage, speed or otherwise to doing this?

A) Nobody in his right mind would pass parameters to a method
which the method does not need and use (circumstances
may dictate deviations from this).

B) A non-static method has the object itself as an implicit
parameter.

Conclusion: If the object is not needed/used by the method,
making it static helps to clearly indicate this fact.

In addition it may help the compiler in figuring out that
no dynamic binding is necessary when calling the method.
This should result in slightly faster an slimmer code.

Harald.
 
Joined
Oct 15, 2007
Messages
1
Reaction score
0
Advantages of Static Method - Shrinand Vyas

Hi,

I hope the following comments help.

If you are operating on any static members of the class. The methods using them must be static.

The advantage here is many classes can share the same member using same static method without the need of instantiating the class.

This brings important memory savings in and eliminates complex measures that we have to take while using shared memory - (static members are shared between objects of the same class)

Shrinand Vyas
University of Houston
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top