Java Forum / First Aid / May 2005
Calling a static member from an instance
cdx - 21 Apr 2005 02:58 GMT If I am in the instance method of a class and want to call a static method, what is the correct notation?
Do I do somrthing like - this.getClass().staticMethod(); ?????
klynn47@comcast.net - 21 Apr 2005 04:29 GMT If the method is in the same class, you can just call it like an instance method.
cdx - 21 Apr 2005 15:53 GMT > If the method is in the same class, you can just call it like an > instance method. "If the method is in the same class, you can just call it like an instance method."
I get the following warning when I do this
The static method tableName() from the type PersistentDomain should be accessed in a static way
What I'm trying to do is have subclasses of PersistentDomain implement the static method tableName to be stored to. The instance code in PersistentDomain has a generalized method that uses the tableName from the specific instance to store the data.
I hope that gives you an idea of what I am trying to do.
Chip
Nigel Wade - 22 Apr 2005 10:08 GMT >> If the method is in the same class, you can just call it like an >> instance method. [quoted text clipped - 6 lines] > The static method tableName() from the type PersistentDomain should be > accessed in a static way You can make the compiler happy by changing the invocation to PersistentDomain.tableName().
 Signature Nigel Wade, System Administrator, Space Plasma Physics Group, University of Leicester, Leicester, LE1 7RH, UK E-mail : nmw@ion.le.ac.uk Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Chris Smith - 23 Apr 2005 14:08 GMT > I get the following warning when I do this > [quoted text clipped - 5 lines] > PersistentDomain has a generalized method that uses the tableName from > the specific instance to store the data. In that case, tableName() should not be a static method. Polymorphic dispatch is only available for instance methods.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
cdx - 24 Apr 2005 05:02 GMT >>I get the following warning when I do this >> [quoted text clipped - 8 lines] > In that case, tableName() should not be a static method. Polymorphic > dispatch is only available for instance methods. That's kind of limiting isn't it? Yes I want subclasses to implement it.
Chris Smith - 25 Apr 2005 15:22 GMT > > In that case, tableName() should not be a static method. Polymorphic > > dispatch is only available for instance methods. > > That's kind of limiting isn't it? Yes I want subclasses to implement it. No, it's not limiting. Instance methods are not required to access instance state. However, if you want behavior to depend on which instance is used to call the method, then you want an instance method and not a static one.
The entire point of a static method is that it is not called on a specific instance. In that case, what you are asking for would be non- sensical.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
cdx - 19 May 2005 02:54 GMT >>>In that case, tableName() should not be a static method. Polymorphic >>>dispatch is only available for instance methods. [quoted text clipped - 9 lines] > specific instance. In that case, what you are asking for would be non- > sensical. Chris, I think my comment on limiting had more to do with the fact that you cannot use "polymorphic dispatch" on the static methods of a class. I'm a Smalltalker and I assumed that you could use inheritance on the static side as well. I'm trying to implement a framework that I built in Smalltalk in Java, but since Java doesn't support this type of object-oriented usage, I guess I need to rethink how things gets done in Java. Using Smalltalk I'm letting the class implement the framework and use hierarchy to change things such as what sql table a class should load from.
Was not allowing for "polymorphic dispatch" on the static side an intentional design of Java? I'm assuming that there was a reason that it was done this way.
cdx
Chris Smith - 19 May 2005 13:50 GMT > I think my comment on limiting had more to do with the fact that you > cannot use "polymorphic dispatch" on the static methods of a class. Right. As I said, though, there's an easy solution to that. Make it an instance method. If you want polymorphic dispatch, then you must have an instance; otherwise, there'd be nothing to determine which method is dispatched to. If you have an instance, then you don't need a static method.
In fact, needing an instance to sensibly invoke a method is the very DEFINITION of an instance method.
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
cdx - 19 May 2005 23:37 GMT >>I think my comment on limiting had more to do with the fact that you >>cannot use "polymorphic dispatch" on the static methods of a class. [quoted text clipped - 7 lines] > In fact, needing an instance to sensibly invoke a method is the very > DEFINITION of an instance method. I came to the conclusion that I needed to do that strictly for the functionality of polymorphism. However the issue really comes down to not having polymorphism on the static side. Regardless of the example I gave you, I have found very good opportunities to use it on the class side in Smalltalk and find the lack of it in Java restricting.
The solution that I am implementing is definately a hack because I am instantiating objects purely for the purpose of polymorphism of what would have been static methods. Now of course they are instance, but only because I cannot do this on the static side.
Chris Smith - 20 May 2005 00:02 GMT > I came to the conclusion that I needed to do that strictly for the > functionality of polymorphism. However the issue really comes down to [quoted text clipped - 6 lines] > would have been static methods. Now of course they are instance, but > only because I cannot do this on the static side. So we get the point that you feel like you know what you want. Please enlighten us. If you didn't have an instance, how would you expect the VM to decide the polymorphic dispatch. For example, let's say you have:
class A { ... } class B extends A { ... } class C extends A { ... }
And all three define a method called foo(). And you write A.foo(). If you wanted polymorphism, then presumably the VM would somehow know that you really wanted B.foo() to be called. How would it know that?
 Signature www.designacourse.com The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation
cdx - 20 May 2005 11:25 GMT >>I came to the conclusion that I needed to do that strictly for the >>functionality of polymorphism. However the issue really comes down to [quoted text clipped - 18 lines] > you wanted polymorphism, then presumably the VM would somehow know that > you really wanted B.foo() to be called. How would it know that? Here's the scenario. in ClassA I have a static method loadInstanceAt(aKey).
In that method, I go to SQL to load the data and create an instance of the class. ClassA implements sqlTableName() and persistentFields() as abstract static methods ClassB implements sqlTableName() and persistentFields() as the tableName for that class (say "ClassBTable" and an Array of columns in that sql table ("id", "name", "salary").
By calling ClassB.loadInstanceAt(1) the static code in ClassA could construct a SQL statement (using persistentFields() and sqlTableName() from ClassB), issue the sql statement, construct an instance of ClassB (that is the Class that I called the static on) and return it.
This is done all of the time in Smalltalk and makes for polymorphism on the static side. It works great and I'm wondering why polymorphism on the static side is not implemented in Java
hawat.thufir@gmail.com - 24 May 2005 04:18 GMT ...
> Here's the scenario. > in ClassA I have a static method loadInstanceAt(aKey). [quoted text clipped - 5 lines] > ClassB implements sqlTableName() and persistentFields() as the tableName > for that class (say "ClassBTable" and an Array of columns in that sql
> table ("id", "name", "salary"). > [quoted text clipped - 5 lines] > This is done all of the time in Smalltalk and makes for polymorphism on > the static side. It works great and I'm wondering why polymorphism on
> the static side is not implemented in Java I thought that calling a static member from an instance showed a mis-understanding of object oriented programming; you're saying it's specific to java (and C#, I imagine). I'm not sure what to say, but it's certainly food for thought.
-Thufir
cdx - 25 May 2005 00:07 GMT Oh I wish I never gave that as an example.
Forget about calling a static from an instance, but instead focus on the ability to use polymorphism on the static side. That is what my previous example was all about and that is what I'm trying to discuss. The fact that you cannot use polymorphism on the static side is what I'm questioning. Is there any reason why this was done in Java or was that ability just not "recognized"?
> I thought that calling a static member from an instance showed a > mis-understanding of object oriented programming; you're saying it's > specific to java (and C#, I imagine). I'm not sure what to say, but > it's certainly food for thought. > > -Thufir hawat.thufir@gmail.com - 27 May 2005 07:41 GMT > Oh I wish I never gave that as an example. > [quoted text clipped - 11 lines] > > > > -Thufir the example you have given is, for me, arcane. perhaps it'd be clearer, for me, if you gave something more accesible. perhaps legends of zelda, or star wars, something low-brow. I just don't have the SQL to really know what you're on about, sorry.
-Thufir
Tor Iver Wilhelmsen - 21 Apr 2005 08:03 GMT > Do I do somrthing like - this.getClass().staticMethod(); ????? No, that would call a method in the class Class.
To call a static method you should preferrably use ClassName.methodName().
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|