Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method in abstract class

 
Cairo Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"cairo cairo", please check your private messages for an important administrative matter.
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have very pathetic solution to this but atleast it will work...
PASS THE CLASS NAME...! ! !



I am so sorry for this solution ..
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are all the methods in the ServerFunction classes static. Why can't the prinMyName() method be an instance method?
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Cairo Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cairo Jackson
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic