Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > static methods in interfaces

Reply
Thread Tools

static methods in interfaces

 
 
ballpointpenthief
Guest
Posts: n/a
 
      12-07-2006
Is there a nice workaround for this?
I wanted to have a some static methods in an interface, but as you
probably know that's not legal.

This is what I'm trying to do:
Each object that implements this interface needs to handle certain
tasks (File parsing/writing, small JFrames etc...) in a way that is
specific to that particular class.
When the objects are being passed around, they are being passed around
with the interface type, but I need to be able to invoke their specific
methods.

Static methods declared in the interface would be ideal, but for some
reason this isn't allowed (why not?)
I'm guessing any workarounds may involve static variables, reflection
or C++.

Thanks,
Matt

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-07-2006
ballpointpenthief wrote:

Sub: static methods in interfaces

<sscce>
public interface TheInterface {
/** Left to implementers. */
public abstract Object theMethod();
}

class TheClass implements TheInterface {
public Object theMethod() {
return this;
}
}
</sscce>

Andrew T.

 
Reply With Quote
 
 
 
 
ballpointpenthief
Guest
Posts: n/a
 
      12-07-2006
Firstly,
I can't see how the use of the abstract keyword in an interface
changes anything at all here. Could someone explain this?

Secondly, I think you may have skimmed my post:
For example, one of the methods will be a JFrame getEntryForm()
which will return a frame specific to the class in order to create a
new instance. This obviously should be a static method - there might
not even be any instances of that class. (Short of creating one by
invoking it's constructor, calling the method, and discarding it
after.)

Hope I've not misunderstood anything,
Matt

Andrew Thompson wrote:
> ballpointpenthief wrote:
>
> Sub: static methods in interfaces
>
> <sscce>
> public interface TheInterface {
> /** Left to implementers. */
> public abstract Object theMethod();
> }
>
> class TheClass implements TheInterface {
> public Object theMethod() {
> return this;
> }
> }
> </sscce>
>
> Andrew T.


 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      12-07-2006
ballpointpenthief wrote:
> Is there a nice workaround for this?
> I wanted to have a some static methods in an interface, but as you
> probably know that's not legal.
>
> This is what I'm trying to do:
> Each object that implements this interface needs to handle certain
> tasks (File parsing/writing, small JFrames etc...) in a way that is
> specific to that particular class.
> When the objects are being passed around, they are being passed around
> with the interface type, but I need to be able to invoke their specific
> methods.
>
> Static methods declared in the interface would be ideal, but for some
> reason this isn't allowed (why not?)
> I'm guessing any workarounds may involve static variables, reflection
> or C++.
>
> Thanks,
> Matt
>


You seem to be breaking the object oriented behaviour of your model.

However, if you are sure that you want to do this, the way to do this is
to use "instanceof" or equivalents, and casting:

Shape shape = getShape();
if (shape instanceof Square) {
Square square = (Square) shape;
square.setSide(2);
}
shape.drawShape();

Hope this helps,

Rogan
 
Reply With Quote
 
ballpointpenthief
Guest
Posts: n/a
 
      12-07-2006
Hopefully this is a SSCCE:
(I have reason not to use an abstract class.)

public interface TheInterface {
public static String getSomethingReleventToClass();
}

public Class AClass implements TheInterface {
private String somethingReleventToClass = "This will be different
in each class";
public static String getSomethingReleventToClass() {
return somethingReleventToClass;
}
}

public Class Application {
private TheInterface someClass;
public Application() {
someClass.getSomethingReleventToClass();
}
}

Cheers,
Matt

> Andrew Thompson wrote:


> > <sscce>
> > public interface TheInterface {
> > /** Left to implementers. */
> > public abstract Object theMethod();
> > }
> >
> > class TheClass implements TheInterface {
> > public Object theMethod() {
> > return this;
> > }
> > }
> > </sscce>


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      12-07-2006
ballpointpenthief wrote:

> I can't see how the use of the abstract keyword in an interface
> changes anything at all here. Could someone explain this?


It doesn't; all methods declared in interfaces are necessarily public and
abstract -- whether you say so or not


> For example, one of the methods will be a JFrame getEntryForm()
> which will return a frame specific to the class in order to create a
> new instance.


I think you may be looking for the "Factory Object" pattern. Google (or
whatever search engine you favour) will turn up the details easily.

-- chris


 
Reply With Quote
 
Jhair Tocancipa Triana
Guest
Posts: n/a
 
      12-07-2006
ballpointpenthief writes:

> When the objects are being passed around, they are being passed around
> with the interface type,


If this were the case. What is the problem with that?

> but I need to be able to invoke their specific methods.


You can invoke the methods in the classes which implement the
interface.

> Static methods declared in the interface would be ideal, but for
> some reason this isn't allowed (why not?)


You are totally confused. Interfaces just specify the signatures of
the methods that *must* be implemented by classes which implement the
interface.

--
-- Jhair
 
Reply With Quote
 
Rogan Dawes
Guest
Posts: n/a
 
      12-07-2006
ballpointpenthief wrote:
> Hopefully this is a SSCCE:
> (I have reason not to use an abstract class.)
>
> public interface TheInterface {
> public static String getSomethingReleventToClass();
> }
>
> public Class AClass implements TheInterface {
> private String somethingReleventToClass = "This will be different
> in each class";
> public static String getSomethingReleventToClass() {
> return somethingReleventToClass;
> }
> }
>
> public Class Application {
> private TheInterface someClass;
> public Application() {
> someClass.getSomethingReleventToClass();
> }
> }
>
> Cheers,
> Matt


Ok, notice that in your code here, you actually have an INSTANCE of
TheInterface in class Application. So, the way you are invoking
getSomethingRelevantToClass is correct, IF you were not attempting to
define getSomethingRelevantToClass statically.

From your previous emails, it sounded like you actually only wanted to
pass the class name around, or Class object, to be precise. As far as I
know, the fundamental problem with your approach is that since you
cannot define static methods on an interface, you cannot do something like:

interface TheInterface {
public static String getSomethingRelevantToClass();
}

and hope to do:

TheInterface.getSomethingRelevantToClass();

However, I think that you will find that they way you wrote it above is
99.9% correct, if you have INSTANCES of classes implementing
TheInterface, just take out the "static" when defining your TheInterface
class.

This is actually entirely standard OO programming in Java.

Rogan
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      12-07-2006
ballpointpenthief wrote:
> Hopefully this is a SSCCE:


Only something that compiles can be an SSCCE, but it is an SSCE (static
self-contained example).

> (I have reason not to use an abstract class.)
>
> public interface TheInterface {
> public static String getSomethingReleventToClass();


Why does this need to be static?

> }
>
> public Class AClass implements TheInterface {
> private String somethingReleventToClass = "This will be different
> in each class";


If it is associated with the class, why isn't this static?

> public static String getSomethingReleventToClass() {
> return somethingReleventToClass;
> }
> }
>
> public Class Application {
> private TheInterface someClass;
> public Application() {
> someClass.getSomethingReleventToClass();
> }
> }


I would just make getSomethingRelevantToClass an instance method, in
both the interface and the implementation. Instance methods can
reference static variables just as well as static methods can. On other
hand, in any implementation in which somethingRelevantToClass is
correctly named, I would make it a static variable.

There can't be any problems with needing to call the method through the
interface without an object existing. Interfaces are inherently
abstract, so you can't do anything much with them other than access
constants unless you have an instance of an implementing class.

Patricia
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-07-2006
Patricia Shanahan wrote:
> ballpointpenthief wrote:
> > Hopefully this is a SSCCE:

>
> Only something that compiles can be an SSCCE, but it is an SSCE (static
> self-contained example).


good one

For the OP, the SSCCE is described here..
<http://www.physci.org/codes/sscce>

A.

 
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
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
Avoid calling non-final methods in a constructor: Applies to static methods too? Oliver Wong Java 14 06-13-2006 09:18 AM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 2 05-26-2005 06:25 PM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57