• Post Reply Bookmark Topic Watch Topic
  • New Topic

Invoke a static method.  RSS feed

 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Can there be any situation where one would invoke a static method on an instance?
I thought that there are no advantages of calling a static method on an instance as the object handle gets
replaced with the class name during compilation. I came across some propietary code (which I can't
put up here) where we are doing just this... and I can't understand why, even the documentaion states
that we call this static "method on an instance of the class".

Regards,
Ven.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ven jovovich wrote:I can't understand why, even the documentaion states
that we call this static "method on an instance of the class".

Probably bad wording. Programmers aren't paid for their command of the English language.

However, static methods can be called from instance code. Usually, you write a static method when it stands on its own (ie, it doesn't require any information from the instance). For example, for a "utility" method:HIH

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean like this?

If so, no, there's no good reason to do that. In fact some people consider it a design flaw that Java allows you to do that at all rather than making in a compiler error.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Do you mean like this?

Ah. Good point. I hadn't thought of that.

@ven: If that's what you meant, then forget my post.

Winston
 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew and Winston.

@Winston: what Matthew says is what I meant It's not just the documentation, even the code is... not wrong... just bad design. I agree that documentation sucks and my sincere respect to that programmer, maybe I would have made the same mistake(s).
Neither am I paid for my command over the English lang.

Regards,
Ven.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Format class with a lot of useful utility methods which can also be accessed from Velocity.
Where they are used, there is always a Format insatnce at hand anyway.
Some of the utility methods are actually static, but who cares at that point?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:I have a Format class with a lot of useful utility methods which can also be accessed from Velocity.
Where they are used, there is always a Format insatnce at hand anyway.
Some of the utility methods are actually static, but who cares at that point?

People who are trying to read and understand your code probably care.
Calling static methods from an instance variable is confusing in the same way that starting class names with lower case letters or variable names with upper case letters is confusing.

I don't understand your point about "there is always a Format instance at hand anyway". You don't need (and shouldn't be using) an instance of the class to call the classes static methods so what difference does it make if there is or isn't an instance available?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Developers who think it is OK to call static methods via instances of a class probably think it is OK to use:
- One line, no brace condition statements
- Multiple returns in a method
- Return statements in a void method
- No javadocs
- Different naming standards to those set out in the original Java tutorials (as mentioned by Tony)
- Business logic within exception blocks

Just because you are allowed to do these things, doesn't make them right. Always think of a next developer who will be maintaining your code.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand your point about "there is always a Format instance at hand anyway".


I am using Velocity templates and I put an instance of my Format class into the Velocity context with the name say "format".
In the template I can invoke the Format methods like this

${format.method( ${par1} , ${par2} )}

Here it is where "there is a Format instance at hand anyway".

Good old Velocity predates the JSP/JSTL stuff.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan

Using Velocity doesn't mean the methods accessed via a template have to be static though.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- Multiple returns in a method
- Return statements in a void method


What is wrong with them?

Let us suppose we have a method which has to compute a result.
It is of advantage if the code goes straightforward: where a value is returned on the spot as soon as it is found
as opposed to a complicated multi-level if-construct.


 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Velocity doesn't mean the methods accessed via a template have to be static though.


Nobody said that.

But they can be instance or static methods, they can be accessed the same way from the template, and at this point it is moot to take care.
 
ven jovovich
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, lots to learn here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!