GeeCON Prague 2014*
The moose likes Java in General and the fly likes static method in abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "static method in abstract class" Watch "static method in abstract class" New topic
Author

static method in abstract class

Cairo Jackson
Greenhorn

Joined: Jan 18, 2007
Posts: 14
Friends,
I have a question that I could not solve at the moment, since my codes are long and complicated, so I made a simple case, as the codes below:



OK.. the issue is I have different servers class share and use same functions. Additionally, I need to have something like central control over the functions, so I put these same functions into an abstract class called, ServerFunction.

Since the server (alpha and beta) are all static methods, so I must declare printMyName() as static. But I want the server print out their package class name when using printMyName(). Previously, if it is not the case of static, i use this.getClass(). Now I cannot. Can someone teach me how to solve this?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61304
    
  66

"cairo cairo", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
I have very pathetic solution to this but atleast it will work...
PASS THE CLASS NAME...! ! !



I am so sorry for this solution ..


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Why are all the methods in the ServerFunction classes static. Why can't the prinMyName() method be an instance method?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

I think you're letting yourself get confused by the main method, which is static.

The following code example should show you how you can create the sort of inheritance structure and use it to get the desired results:



There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Cairo Jackson
Greenhorn

Joined: Jan 18, 2007
Posts: 14
Originally posted by Vishal Matere:
I have very pathetic solution to this but atleast it will work...
PASS THE CLASS NAME...! ! !



I am so sorry for this solution ..


Thank you, Vishal Matere. I am thinking of this solution as well, but it lose the spirit/feel of inheritance. If I put ServerAlpha.class in parameter, it feels like they are different classes and I may just create an instance of ServerFunction and use it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

That's really your only option. We've had this conversation many times before. What it boils down to is that static methods forever belong to the class in which they're defined. There's no way for them to know the name of the class they're invoked on, since no such information is encoded by the compiler.


[Jess in Action][AskingGoodQuestions]
Cairo Jackson
Greenhorn

Joined: Jan 18, 2007
Posts: 14
Originally posted by Stevi Deter:
I think you're letting yourself get confused by the main method, which is static.

The following code example should show you how you can create the sort of inheritance structure and use it to get the desired results:



Thank you, Stevi Deter. OK... the story is I have two servers, namely UDPPortServer and TCPPortServer. Since they are servers, all the methods used are static by default. The main method will just be there listening command from prompt. But UDPPortServer and TCPPortServer shares many similar functions/methods, and now I want to put all these methods into a separate abstract class (one, i need to log these methods; two, I no need to modify the codes twice if something wrong) so these two servers can just share.

mm... how do you think?

Thank you again.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Cairo,

Sorry for misunderstanding your constraints.

For the specific issue of logging, I would personally recommend a separation of concerns and using an existing package like Apache Commons Logging to avoid further reinventing the wheel.

Without a deeper description of why you're feeling like you must limit yourself to static methods, I can't give much more useful advice. I would recommend you think about using the static methods to create and use classes that aren't dependent on static methods, if you don't feel you can create your Server classes that way. A contrived example:

Cairo Jackson
Greenhorn

Joined: Jan 18, 2007
Posts: 14
Originally posted by Stevi Deter:
Cairo,

Sorry for misunderstanding your constraints.

For the specific issue of logging, I would personally recommend a separation of concerns and using an existing package like Apache Commons Logging to avoid further reinventing the wheel.

Without a deeper description of why you're feeling like you must limit yourself to static methods, I can't give much more useful advice. I would recommend you think about using the static methods to create and use classes that aren't dependent on static methods, if you don't feel you can create your Server classes that way. A contrived example:



I thinks that's the only way. Thank you, Stevi Deter. Thank you, everyone.
 
GeeCON Prague 2014
 
subject: static method in abstract class