Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

overridnig a static object  RSS feed

 
sharaf navas
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all please solve me this?

--------------------------
public abstract class ActionBase
{
public static String ACTION_NAME=null ;
}
-----------------------------
public class AddEmployeeAction extends ActionBase
{
static
{
ACTION_NAME ="addEmployee";
}
}
-----------------------------------

public class AddGroupAction extends ActionBase{
static
{
ACTION_NAME ="addGroup";
}
}
------------------------------------
String action="addEmployee";
if (action.equals(AddEmployeeAction.ACTION_NAME))
doXXXX();
if(action.equals(AddGroupAction.ACTION_NAME))
doYYYY();
else doErorrMethod();
please help which will be performed
thaks in advance
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one copy of the static variable ACTION_NAME in this program. It looks like the author of this code believed each base class would get its own copy -- this is not the case. Its value depends on which of the two subclasses is loaded second.

This is horrible code -- a horrible thing to do. If this is based on some real programming you're doing, don't do it. Each class needs its own separate static variable, and the uninitialized one in the base class should be removed.

If this is just a test question, then we can talk a little about it, as long as you keep in mind what was said above. The answer depends on whether any other code references either of these base classes -- code in the same class as the code you're showing here, or in other classes. If no code anywhere else in the application mentions the classes AddEmployeeAction or AddGroupAction, then this code should invoke doXXXX(). Otherwise, it depends on which of the two employee classes was loaded last before this code was invoked.
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharaf,

People on this forum will be more prepared to help you if you actually show that you have tried to help yourself a little.

Do you have a Java compiler? I guess you do. So why not try it out for yourself and then if you have a query about the results you get, then post a question?

Regards,
JD
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Static means sinle copy just single copy.

The static instance variable in the base class will be refered by all extended classes. So any chane in the sub classes will be reflected in base class.

And Static is class level not object level, so any chane in the static value in one of the created objects will affect all other dependent objects. So careful to use this in real situations.

and for the question, it depends on the last created object before checking the if condition as it will modify the static reference.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!