wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes static variables in instantiable classes (vs abstract vs interface) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variables in instantiable classes (vs abstract vs interface)" Watch "static variables in instantiable classes (vs abstract vs interface)" New topic
Author

static variables in instantiable classes (vs abstract vs interface)

Greg Blajian
Greenhorn

Joined: Aug 31, 2005
Posts: 5
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

Joined: May 02, 2005
Posts: 209
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.


With Best Regards,
Shyam Prasad Murarka
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Greg Blajian
Greenhorn

Joined: Aug 31, 2005
Posts: 5
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.)


[Jess in Action][AskingGoodQuestions]
Greg Blajian
Greenhorn

Joined: Aug 31, 2005
Posts: 5
Thank you Ernest, that answered my question very clearly and concisely. I really appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables in instantiable classes (vs abstract vs interface)
 
Similar Threads
Passing by value vs. reference
static vars vs local vars
Thread Join vs isAlive() and wait()
Thread Vs. Thread
Vector Vs Expandable Array