Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables in instantiable classes (vs abstract vs interface)

 
Greg Blajian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following which way is the best in terms of "best practice" and speed? All of the calls work fine, but is one better than the other? The only things in any non-implementing class are static variables. Eclipse complains about instantiating a class to call a static variable and I can see why it would, but is there some benefit in speed using an abstract class over a class, or over an interface? To me the interface makes the most sense but I can't seem to come up with a justification that sounds good, especially if there is some penalty in speed to using an interface as opposed to a class (abstract or otherwise).

<code>
class Goofy1
{
public static final String FIRST = "Mickey";
public static final String SECOND = "Donald";
public static final String THIRD = "Goofy";

public Goofy1()
{
super();
}
}

public interface Goofy2
{
String FIRST = "Mickey";
String SECOND = "Donald";
String THIRD = "Goofy";
}

public abstract class Goofy3
{
public static final String FIRST = "Mickey";
public static final String SECOND = "Donald";
public static final String THIRD = "Goofy";

public Goofy3()
{
super();
}
}

public class GoofyImpl
{
/**
* @param args
*/
public static void main(String[] args)
{
Goofy1 myGoofy1 = new Goofy1();
System.out.println("Disney's first: " + myGoofy1.FIRST);
System.out.println("Disney's second: " + Goofy2.SECOND);
System.out.println("Disney's third: " + Goofy3.THIRD);
System.out.println("Disney's favorite: " + Goofy1.THIRD);
}
}
</code>

TIA
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
After reading this post I have been wondering about the speed benefit of either using classes OR abstract classes OR interfaces! I never knew that there was speed benefit for using different implementations. What I know is that the use of those different styles are purely based upon the design of the code and its need and NOT its speed. That's what I think but I may be easily wrong so I really hope somebody comes up to clarify this.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general advice is to write first for human consumption. Make code that clearly communicates what it is trying to do and is economical for humans to change over time. When you can prove there is a performance issue by observing running code, fix it.
 
Greg Blajian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While I agree with you in general, when there is an opportunity or a need to improve the code for performance gains is there some advantage to one method over another? I ask because of something I read on this site Mindprod. If I just believed it out of hand I wouldn't even have asked but, although that may have been true at some time in past incarnations of java, is it still true now? Was it ever true?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roedy Green's page at mindprod says that interfaces are "Slow, requires extra indirection to find the corresponding method in the actual class. Modern JVMs are discovering ways to reduce this speed penalty." He's talking about method dispatch and not static final variables. There's no difference at all in the performance of looking up a static final variable in an interface, abstract, or concrete class -- they're all exactly the same. There is a difference in the mechanism of how interface methods are looked up compared to methods inherited from an abstract class, but the quote above is quite dated. The actual performance difference these days is tiny-to-nonexistent.

In any case, the statics in all your examples are compile-time constants. Their values are precisely known to the compiler when the code is compiled. Therefore, the values will be hardcoded into any class files that reference them, meaning that not only isn't there a difference among your three choices, but there actually isn't any lookup done at runtime at all!

As Stan says, other considerations are more important -- Roedy Green's chart contains some good things to consider (except for the performance one.)
 
Greg Blajian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ernest, that answered my question very clearly and concisely. I really appreciate it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic