Reflection and the field.set method

OK, I've made 2 classes, a ClearParent and ClearChild (which extends ClearParent). I'd like to have a "clear" method on the parent which dynamically sets all the fields to null. I'd like to be able to have any child that inherits from the parent be able to call clear and dynamically have all it's fields set to null.
Part of the restriction that I'm facing is that the fields are Objects (Integer) but the getter and setter methods take and return types (int). So I can't just loop through the Methods and call the setters with null.
I'd like to be able to loop through the fields and call set for all the fields with the parent class.
I'm inserting the code that I have for the parent and child classes at the end. Basically, the problem that I'm seeing is that if I do a
this.getClass().getName()
from the parent (when clear is called from the child) it shows me the child name ("ClearChild"). But when I try to do the
field.set(this, null)
it tells me this:
Class ClearParent can not access a member of class
ClearChild with modifiers "private"
How come when I get the name it tells me that it's the child but when I pass "this" to the set method, it says that it's the parent?
Any one know what's going on here? Is there anyway that I can have the parent set all the fields to null?
Thanks in advance.
Here's the code:
ClearParent
import java.lang.reflect.*;
public class ClearParent {
    public boolean clear() {
        try {
            System.out.println(this.getClass().getName());
            Field[] fields = this.getClass().getDeclaredFields();
            Field   field;
            for (int i = 0; i < fields.length; i++) {
                field = fields;
field.set(this, null);
} catch (Exception e) {
e.printStackTrace();
return true;
ClearChild
public class ClearChild extends ClearParent {
private Float f;
public ClearChild() {
super();
public float getF() {
if (f == null) {
return 0;
return f.floatValue();
public void setF(float f) {
this.f = new Float(f);
public static void main (String[] args) throws Exception {
ClearChild cc = new ClearChild();
cc.setF(23);
cc.clear();
System.out.println("cc.getF: " + cc.getF());

It is an instance of ClearChild that is being used so
that class name is ClearChild. However, the method
exists in the parent class and thus cannot act upon a
private field of another class (even if it happens to
be a subclass).Ahh...makes sense.
Why don't you just override clear in the child?We were trying to avoid this because we run into problems in the past of people adding fields to the class, but not adding to the clear, and things not being cleared properly.
I talked it over with the guys here and they have no problem making the fields protected instead of private. If it's protected, then the parent has access to it and my sample code works.
Thanks for your help KPSeal!
Jes

Similar Messages

  • What version of iPhoto.  iPhoto 11 9.4.3 (720.91) -  What version of the Operating System. OSX 10.8.3 -  Detail.  I Attach my camera and the fields where the photos usually show up are empty and at the top of the screen it says all photos from camera are

    What version of iPhoto. iPhoto 11 9.4.3 (720.91)
    What version of the Operating System. OSX 10.8.3
    Detail.  I Attach my camera and the fields where the photos usually show up are empty and at the top of the screen it says all photos from camera are downloaded. But they are not. They can't be.
    I fixed the problem somewhat myself by removing the miniSD and manually inserting it. It did not populate automatically. I had to open iPhoto and then it populated only the most recent photos I had taken which fortunately were the only ones I need. No error messages this seems to be a new occurrence.
    Is there a way to just turn iPhoto off and download my photos to the folder of my own choosing?

    Sure - in the iPhoto prefercences set action when a camera is ocnnected to Image Capture
    LN
    Note - your cute formatting really does not add anything and makes your post hard to read and follow - try straight typing with no cute formatting in the future

  • Allowing the field "Payment method supplement" to be changeable in FB02

    Hi All,
       I have a requirement which is to allow the field "Payment method supplement" to be changeable in FB02. It is currently not allowed for change. How will I make this changeable? Is customizing enough? Or program enhancement must be done? Please advise the steps to do this.
    Thanks!

    Hi
    Please use tcode OB32 to make Payment method supplement field editable in FB02 transaction.
    In the screen "Change view: Document change rules: overview: screen" click F5 or New entries.
    In the subsequent screen enter * BSEG-UZAWE* in the Field Name and update all other fields like account type, transaction, company code.
    Activate the Field can be changed check box and save.
    Now after doing this change, when you try changing the document in FB02, the payment method supplement field can be changed.
    Please update the fields Account type, transaction with relevant values.
    Hope this will help you...
    Regards,
    Praisty

  • Stored application and the ExtendedAppAttributes.isStartable() method

    Hi,
    According to MHP 1.1.3 sub-chapter 10.14.2 the application could be launched also when:
    - not_launchable_from_broadcast is set to "1"
    - is_launchable_with_older_version is set to "1"
    - some older version of this application is stored (but not the current version)
    So it seems that the ExtendedAppAttributes.isStartable() method shall return true in this case.
    However the specification describing this method says nothing about the is_launchable_with_older_version flag. Instead of it says:
    "+An Application is not startable if ... The application is signalled as not_launchable_from_broadcast and is not stored...+".
    Does it mean that application is stored also when the is_launchable_with_older_version flag is set to "1" and any older version of this application is stored (but not the current version)? And the ExtendedAppAttributes.isStored() method shall return true in this case?
    Or the ExtendedAppAttributes.isStartable() method has nothing common with the possibility of application launching? That is, even if it returns false the application may be launched/started by the terminal/another xlet.
    Regards,
    Adam Lessnau

    Hi cf090,
    Audit logs are stored in a hidden, dedicated arbitration mailbox. We can access them by using the ECP Auditing Reports page, the Search-AdminAuditLog or New-AdminAuditLogSearch cmdlet.
    For more information, here is an article for your reference.
    Overview of Administrator Audit Logging
    http://technet.microsoft.com/en-us/library/dd335052(v=exchg.141).aspx
    Hope it helps.
    If there are any problems, please feel free to let me know.
    Best regards,
    Amy
    Amy Wang
    TechNet Community Support

  • Parts Requirement Create Order Error:Validation Failed for the field - Shipping Method DHL

    Trying to create Parts Requirement.
    Error message on clicking "Create Order" button: "Validation Failed for the field - Shipping Method DHL"
    Please suggest the Setup that needs to be done for this.

    Kinck
    The same script worked for me in vision (R12.1). You may have issues with your defaulting of a number of attributes that you are not passing in the API (like price list, line type and so on). You may want to check them.
    Thanks
    Nagamohan

  • I have a new 160GB iPod Classic. I often listen to it for many hours at a time. Even with Itunes and the Classic set on shuffle it repeats songs often and only plays a small fraction of my songs. Help!            of my

    When listening for hours to my new Clasic 160 with several thousand songs and both itunes and the Classic set to shuffle all songs it only plays a small fraction of my library and plays many songs several times over that period. It also seems to pick a small different group of songs to repeat without touching many of most of my songs each time I start listening. Over 98% of the music is legally owned in my CD collection. A few are down loads which play! There should be no issues with ownership. Please help. As much as I like all my tunes I want to actually hear them all. There are many many albums that have never been played.

    Okay, well we've eliminated a couple of things.
    One thing I have noticed about the iPod Classic: I suspect that "shuffle" is actually a Playlist, not a real shuffle. The Classic is not as powerful as iTunes or a desktop/computer, so it cannot truely "shuffle songs". I suspect what happens is that every Sync of the Classic with its iTunes Library causes a revised "shuffle playlist" to be put on the iPod and then, when you choose Shuffle play, it actually uses that special hidden "shuffle playlist".
    So one trick is to Sync your iPod with iTunes frequently. This would change the "shuffle playlist". Another trick would be to create your own Playlists, either by adding what you want to a Regular Playlist or by creating a Smart Playlist which adds and removes songs by itself.
    Smart Playlists: for example, you can create a Smart Playlist (in iTunes) which contains a random selection of all songs, or a random selection of a particular group of songs, provided the song has not been played for a period of time, say six months, or 300 days. Every Sync of the iPod with iTunes would remove any song from that Smart Playlist if it has been played. That same song cannot go back into the Playlist for a period of six months, or whatever you choose.
    This way, you can reduce the likelyhood of hearing the same songs too often. This brings me to another point. If you have the same song, on different albums, then it's possible that you are hearing that same song but from both albums.
    Try this: in your iTunes Library,
    look for and click on the big + button and choose New Smart Playlist..., if you do not have the Sidebar displayed, switch to the Playlists view where you will find it in the bottom left corner,
    Set it up to look like this:
    It creates a list of songs that have not been played for fifteen weeks. It excludes videos and in my iTunes Library, it has 527 songs in it. If you would like a smaller list than that, click on the Limit to and fill in the number of songs you want.
    Having set it up, play some songs from it (in shuffle mode if you wish) and watch how it changes . The song must play through to the end of the track before it is classed as "played". If you like it, let it go onto your iPod. You could set up a few Playlists, each with their own rules. Even if a song appears in two different Smart Playlists, if you have the "last played is not in the last" rule in both lists, then playing the song will eliminate it from both lists. None of this removes songs from the iPod (unless you use Sync selected Playlists...).

  • Mapping and Monitoring all the User and the Field exits

    Hello Dears,
    Are possible, with the Solution Manager to map and monitor all the user and the field exists existing in my ECC6 Productivity Environment?
    Anyone has some documentation?
    Regards to all.
    FS.

    Hello Gurus,
    Someone has any information about this question?
    Regards to all.
    FS.

  • I'm having problems with iPhoto and the facebook set-up.  Every time I attempt to do so it says an error has occured and try again later.  I have not seen a solution posted.  Therefore, how does one contact Apple to solve the problem?  I'm new to the worl

    I'm having problems with iPhoto and the facebook set-up.  Every time I attempt to do so it says an error has occured and try again later.  I have not seen a solution posted.  Therefore, how does one contact Apple to solve the problem?  I'm new to the world of iMac but I find it a bit frustrating trying to solve basic issues/problems:(

    For what it's worth, you posted this in 2011, and here in 2014 I am still having this same issue. Over the last two days, I have had to unlock my apple account 8 times. I didn't get any new devices. I haven't initiated a password reset. I didn't forget my password. I set up two factor authentication and have been able to do the unlocking with the key and using a code sent to one of my devices. 
    That all works.
    It's this having to unlock my account every time I go to use any of my devices. And I have many: iMac, iPad, iPad2, iPad mini, iPhone 5s, iPod touch (daughter), and my old iPhone 4 being used as an ipod touch now.  They are all synced, and all was working just fine.
    I have initiated an incident with Apple (again) but I know they are just going to suggest I change my Apple ID. It's a simple one, and one that I am sure others think is theirs. I don't want to change it. I shouldn't have to. Apple should be able to tell me who is trying to use it, or at least from where.
    Thanks for listening,
    Melissa

  • The volume icon on my ipod won't disappear and the volume sets itself at full.

    The volume icon on my ipod touch won't disappear and the volume sets itself at full. I have tried a master reset etc, but it doesn't want to cooperate. Any ideas?

    Try:
    - A reset. Nothing is lost
    Reset iPod touch: Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Go to Settings > General > Reset and tap Reset All Settings.
    All your preferences and settings are reset. Information (such as contacts and calendars) and media (such as songs and videos) aren’t affected.
    - Restore from backup
    - Restore to factory settings/new iPod
    If you still have a problem that indicates a hardware problem and an appointment at the Genius Bar of an Apple store is in order.
    I do not know what a "master reset" is.

  • I need to  know the name of the database table and the fields in that table

    hi,
    i need to I need to  know the name of the database table and the fields in that table for the following fields of the front end .
    1) incident details.
    2) ownership details
    3) injury type
    4) % of investigation completed withen 7 days.
    5) count of incident type
    6) cost of workers compensation claim.
    7) injury resulting from for workers compensation claim
    8) investigation free text.
    9) investigation contribution factors.
    10) investigation root cause.
    11) investigation root cause free text
    12) employee risk assesment
    13) protential infrigment notice issued
    14) actual infrigment notice issued.
    15) actual infrigment notice reference number.
    16)vehicle damaged text.
    18) when the incident occured.
    thanks and regards,
    pronoy .

    Hello,
    Check CCIHT* under se16 and search for relevant information
    Thanks
    Jayakumar

  • Loan licensing process, and the configuration setting

    Dear Guru's,
    Can any one please explain me about loan licensing process, and the configuration setting?
    Regards,
    Pratap

    Hi
    Loan Licensing is a process mostly used in Pharmaceutical Industry in India, whereby the company which owns a particular brand or patent, lends the license to another manufacturer (Due to capacity bottlenecks or due to Cost advantages).
    This is a form of sub-contracting which might involve supply of API (Active Pharmaceutical Ingredients) or otherwise. In case some of the raw materials (Mostly Active Ingredients) are supplied by the Company it would follow sub-contract process, else it could be an outright purchase process.
    Hope this clarifies your question on Loan Licensing.
    ... Pl
    with kind regards
    Sundar

  • CS3 asked serial number suddenly and the box-set serial number not work.

    CS3 asked serial number suddenly, and the box-set serial number not work.

    Elton Adobe which web browser are you utilizing?  Are you signed into http://www.adobe.com/ when you attempt to contact our support team directly at Contact Customer Care.
    I would recommend utilizing a different web browser and contacting our support team directly as suggested by KGlad in message #6.

  • The best way to remove the USB and the head set( U...

    i have nokia n73 and i want to know if there are some instruction on how to connect the USB and the head set to the phone and how to disconnect them , because sometimes i face some difficulty in con/disconnect them and i am afraid of dameging them , if there is a link to a video it would be better..

    And this thread was titled URGENT right?
    shocking.
    I just pull it out.
    I've never had a problem with these things.
    Obviously if you ram it back in again you can cause problems so be careful there.
    My posts are my opinion and in no way the direct views of Nokia.
    If my posts are helpful, please give me some KUDOS using the green star on the left.

  • When downloading application updates, which are over a GB, my MacBook Air keeps logging me out and I have to start the download over.  I have the power settings set to never turn off, and the display set to never go to screen saver.

    When downloading application updates, which are over a GB, my MacBook Air keeps logging me out and I have to start the download over.  I have the power settings set to never turn off, and the display set to never go to screen saver.  Any suggestions?

    I have the exact same problem/situation since a few days
    I have not intalled a program lately.
    Any progress Voratima??
    Software  Mac OS X Lion 10.7.5 (11G63)

  • JPA and Entities without setter methods

    Hello,
    maybe a stupid question: I have an entity without a setter methods in order to make it immutable after creation. I�ve written all JPA annotations on method level - here on the getter method. I�ve got the error msg that the setter is required. It is possible to annotate fields without the setter method?
    Thx,
    Thilko

    I've got a number of immutable Entity Beans working, but I annotated them all on the field level, not the method level. I couldn't figure out why anyone would want to annotate them as methods, especially given the fact that JPA still sucks with dates. I store all my dates as longs, and use the getters and setters to convert back and forth from java.util.Date objects. If I annotated the methods, I assume I'd either have to find some way to store Date objects, or have my getters and setters deal with longs.
    It feels a little weird declaring a field private and then annotating it so that external code can change it, but it works fine. Additionally, you only have to write half as many annotations.
    Try annotating fields rather than methods (of course, you have to do each Entity Bean all the same way, no mixing) and see how it works for you.

Maybe you are looking for

  • New ipod will not show in itunes.

    my computer isn't very happy at the moment. it randomly unexpectedly quits when i am using photoshop or quark, or really anything. it could happen right now... it hasn't been happy since it was crashed by my friend. he reinstalled everything, but it

  • Avoid connections.xml in JAR file

    I am using a "JAR Deployment Profile" in JDeveloper 9.0.3 to create a JAR file. How can I prevent that connections.xml becomes a part of the JAR file? tnx -Jan

  • How to display a caracteristic in report painter column

    Hi guys, Got a question about reports using Report Painter, apparently it seems that lines are for caracteristics and columns are for key figures but would it be possible to have columns displaying only caracteristics without any key figure?! If so w

  • Confused - obj.conf

    I am attempting to configure Web Server 6.1 to support an application that uses HTTP 1.0 and expects a content-length header in the response. The response is generated from a php script (index.php). The php script does not generate the header itself.

  • Convert measure dimension to flat measure option not found

    Hi All, I am integrating Essbasse with BI. In the physical layer, I tried to convert measure dimension to flat measures. But when I right clicked my Essbase cube I am unable to find the convert measure dimension to flat measures option . My BI machin