• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overridnig a static object

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Get meta with me! What pursues us is our own obsessions! But not this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic