Reference parent/owner class?

Hi,
I am wondering if there is any way in Java to get the name of the class from which the current object was instantiated?
I.e. something like this.parent.getClass().getName() ?
I know I can do this by passing the instantiating object (as type Object) to the constructor of my class and working from there, but am wondering if there is a cleverer way to do this? Perhaps something with classloaders? Remember I just want to find out the name of the class from which my class was instantiated - i'm not trying to get a full reference to it (though obv this work too if there was some way to do this!)
Thanks,
Adrian

AdrianFitz wrote:
Hi,
I am wondering if there is any way in Java to get the name of the class from which the current object was instantiated?
I.e. something like this.parent.getClass().getName() ?
I know I can do this by passing the instantiating object (as type Object) to the constructor of my class and working from there, but am wondering if there is a cleverer way to do this? Perhaps something with classloaders? Remember I just want to find out the name of the class from which my class was instantiated - i'm not trying to get a full reference to it (though obv this work too if there was some way to do this!)Why do you want to do this? What problem is this supposed to solve?

Similar Messages

  • Need reference to calling class

    Is it possible to get a reference to the class that called a particular method? I have a class with an "Object owner" attribute. I am passing this between several different classes, because they all have to perform some operations on the class, but I want certain methods to execute one way if they are called by the object referenced by this.owner, and a different way if they are not. Is there a way to get calling class without passing it in?

    This design is an attempt to control SQL database connections in a threaded environment....No idea what you are talking about. If you are attempting to span threads with a single transaction then you need to control access to the transaction because multiple threads cannot access one jdbc resource at the same time. That it true regardless of anything else.
    If you just want a easy way to control transactions in a persistence layer across multiple DAOs then the following demonstrates a conceptual way to control this. There are endless variations. And error handling must be dealt with.
      class MyConnection
           private bool isTransaction;
           private Connection conn;
           MyConnection(bool isTransaction)
                 this.isTransaction = isTransaction;
                 if (isTransaction)
                      conn = ....with transaction
                 else
                      conn = ....without transaction
           void commit()
                 if (isTransaction)
                       conn.commit()
                 conn.close();
           void rollback()
                 if (isTransaction)
                       conn.rollback()
                 conn.close();
      class MyCustomerDao
              private MyConnection conn;
              private bool isConnOwned;
              MyCustomerDao(MyConnection conn)
                   if (conn == null)
                       this.conn = new MyConnection(false)
                       isConnOwned = true
                   else
                       this.conn = conn
                       isConnOwned = false
              void commit()
                     if (isConnOwned)
                          conn.commit()
      class MyOrderDao
              MyOrderDao(MyConnection conn)
                   if (conn == null)
                       this.conn = new MyConnection(false)
                       isConnOwned = true
                   else
                       this.conn = conn
                       isConnOwned = false
      // Usage for non-transaction
      MyCustomerDao dao1 = new MyCustomerDao(null);
      dao1.commit()
      // Usage for transaction
      MyConnection conn = new MyConnection(true);
      MyCustomerDao dao1 = new MyCustomerDao(conn);
      MyOrderDao dao2 = new MyCustomerDao(conn);
      // following on dao1 is optional since it has no impact.
      // But the idiom is useful because it allows
      // for calls in nested routines without concern
      // for whether parent is needs to control it.
      dao1.commit();
      conn.commit();

  • Are there any advantages to using a Data Value Reference for LabVIEW Classes?

    Hi
    I recently came across an example where the developer had used a data value reference for the class cluster in LabVIEW.
    What are the advantages of doing this?
    Doesn't the use of LV objects already avoid the creation of multiple copies of data thereby reducting memory usage?
    Thanks
    AD

    LabVIEW's OOP is implemented as a By Value.  This means, as Tst stated, branches in wires could mean copies in the object.  The DVR is a way to make it By Reference.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Regular Expression back-reference in character class

    I am trying to capture quoted text (excluding the quotes) with the following pattern:
    "(['\"])([^\\1]+)\\1"
    The input string might look like:
    "That's strange"
    or:
    'valid "regex" pattern'
    I get an exception when trying to compile the pattern, because of the back-reference within character-class brackets: [^\1]
    This pattern worked with the org.apache.oro package. Is this a bug in the 1.4 Pattern class? Can you offer an alternative solution?
    Thanks,
    Tony

    Thanks for the reply. Apparently, my pattern wasn't working as I thought with ORO. The pattern you suggested, however, wouldn't do what I need either, because it would match anything between the first and last quote character. I don't want to match the string you included in your reply, because it includes the same quote character that encloses the entire string. If you think about my pattern again, you'll notice that the first capture group should match either a single or double quote character. Then the back-reference should be the exact character (single character) that was matched and captured. So the second capture group should be any number of characters, as long as they don't match the first capture group character, followed by the exact string matched in the first capture group (the single character). You probably are aware that when one or more characters are included in square brackets [], that it specifies a match of a single character that is found anywhere within that character class. So if the first capture group happened to match more than one character (impossible due to the pattern in that grouping), the following character class [^\1] should match any character EXCEPT any of the ones matched in the first capture group...right? There must be a bug in ORO that doesn't match correctly per my description, but it seems there is a bug in JDK 1.4 Pattern that will not even compile it. Just to re-iterate...if \1 matched "abc", [^\1] should match any single character EXCEPT a or b or c. I can get what I want, though, if I do it in 2 matches:
    Pattern pat = Pattern.compile("(['\"])(.*)");
    Matcher mat = pat.matcher(sText);
    mat.matches();
    String sQuote = mat.group(1);
    String sRem = mat.group(2);
    pat = Pattern.compile("([^" + sQuote + "]*)" + sQuote);
    mat = pat.matcher(sRem);
    mat.matches();
    String sWhatIWanted = mat.group(1);
    Thanks,
    Tony

  • Correct references to other classes

    I'm working on a student project and I'm in some troubles.
    I have a schema "2010_12209" on faculty 11g Oracle DB.
    I have successfully loaded Class A (that has no references to other class) in Oracle DB using SQL Developer.
    Then I try to load Class B (that has references to class A) and I get error: java.sql.SQLException: ORA-24344: success with compilation error ORA-06512: at line 1
    which I'm pretty sure that's from the incorrect classes references.
    So what's the correct syntax to properly reference to class A
    ex: 2010_12209.A or ???

    References to other classes will be links only if the class is in the one of the packages you generate the javadoc in, or if you use the -link option to link your javadoc to other javadocs.

  • [svn] 1101: compiler: fixing a few more sneaky references to renamed classes

    Revision: 1101
    Author: [email protected]
    Date: 2008-04-03 18:17:30 -0700 (Thu, 03 Apr 2008)
    Log Message:
    compiler: fixing a few more sneaky references to renamed classes
    Bugs: n/a
    QA: Yes, Gaurav please retest and send me any remaining failures.
    Doc: No
    Modified Paths:
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/Logger.java
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/SymbolTable.java
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler_en.properties
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler_ja.properties

    Revision: 1101
    Author: [email protected]
    Date: 2008-04-03 18:17:30 -0700 (Thu, 03 Apr 2008)
    Log Message:
    compiler: fixing a few more sneaky references to renamed classes
    Bugs: n/a
    QA: Yes, Gaurav please retest and send me any remaining failures.
    Doc: No
    Modified Paths:
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/Logger.java
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler/SymbolTable.java
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler_en.properties
    flex/sdk/trunk/modules/compiler/src/java/flex2/compiler_ja.properties

  • Parent Child classes

    Hi All,
    I have a really general question that I have not been able to solve.
    Here is my sample code:
    class parent {
    public List mylist
    public parent(){
    child c = new child(this, mylist);
    class child
    private parent p;
    private List mylist;
    private String currentLine ="";
    public child(parent p, List mylist){
    this.p=p;
    this.mylist = mylist;
    BufferedReader in new BufferedReader(new FileReader("test.txt"));
    while ((currentLine = in.readLine()) != null){
    mylist.add(currentLine);
    How do I return mylist in the child class back to the parent class? Or secondly, can I declare a List in the child class and have some way of passing it back to the parent class? I am not very familiar with the parent/child classes. If you can assist, I'd appreciate it. Thanks.

    Actually....the main problem that I have is that I am using multi threading. Right now the problem that I have is that I can't write to the same output file with multiple children threads...not sure why.
    class parent {
    PrintWriter out;
    public parent () {
    out = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
    for (int i=0; i<10; i++){
    childthread ct = new childthread(this);
    public synchronized void Output (String output){
    out.write(output);
    }//end class parent
    class child extends Thread{
    parent p;
    public child (parent p) {
    this.p = p;
    public void run(){
    String thisLine = "test";
    p.Output(thisLine);
    }//end class child
    How can I get all of my 10 threads to write to the same file. Since my parent thread is the one with the output file, each time the child thread calls the p.Output and passes the String..it should printout test in my output file...but it does not. Is there any reason why?
    My output file should display "test" ten times. Please let me know what I am doing wrong...or how I can go about writing multiple threads to the same output file. Thank you.

  • Owner Class and Base Class ?

    hi,
    What is an Owner class and base class?
    thanks,
    Hanu

    Hi,
    the question is related to oracle java class or samething else?
    I dont understand your question.
    Regards,
    Tom

  • Can I test which one owner class Child's instance.

    $ cat Child.java
    public class Child
    public Child(){}
    //how to Know which owner the object,ParentA or ParentB?
    public void testParent()
    $ cat ParentA.java
    public class ParentA
    Child a;
    $ cat ParnetB.java
    public class ParentB
    Child b;
    $

    you may misunderstand the concept of object variables, they are reference to an object. an object may have many references from many other objects, and you can't tell the object itself belongs to what object. in your case, the child actually is a reference rather than an object, and the reference is a value, it can just stay inside the you-called parent objects, and any assignment or method return value is a copy of the reference rather than itself. so it does not make any sense to find its parent.

  • Child form calling a method on it's parent's class.

    I am developing a form-based application. There is one root
    form called "Application" and a few child forms off the root (e.g.,
    Login, CustView). I would like the sibling forms to be able to pass
    contol to each other (e.g., after successful login, I want to make
    Login invisible and CustView visible).
    I wanted the root form to control this interaction, but I'm
    not sure how events in child can be handled in the parent. This is
    a common technique, but in this case, the parent doesn't explicitly
    instantiate the child, so I can't just pass a reference to the
    parent to the child's constructor. I can use this.parentForm to get
    a reference to the parent form, but I can't seem to find the
    parent's methods, just it's gui elements (i.e.,
    this.parentForm.parentMethod() doesn't work).
    Can some tell me how to do this? I am considering a
    FormManager class, which is a singleton class that each form can
    registers with and can use to toggle the visibility of itself and
    its siblings, but I wanted to see if there was an easier way to
    access methods on the class instance associated a parent's form.
    Any suggestions?
    Andy

    "agorman" <[email protected]> wrote in
    message
    news:e2163h$4na$[email protected]..
    >I am developing a form-based application. There is one
    root form called
    > "Application" and a few child forms off the root (e.g.,
    Login, CustView).
    > I
    > would like the sibling forms to be able to pass contol
    to each other
    > (e.g.,
    > after successful login, I want to make Login invisible
    and CustView
    > visible).
    >
    > I wanted the root form to control this interaction, but
    I'm not sure how
    > events in child can be handled in the parent. This is a
    common technique,
    > but
    > in this case, the parent doesn't explicitly instantiate
    the child, so I
    > can't
    > just pass a reference to the parent to the child's
    constructor. I can use
    > this.parentForm to get a reference to the parent form,
    but I can't seem to
    > find
    > the parent's methods, just it's gui elements (i.e.,
    > this.parentForm.parentMethod() doesn't work).
    >
    > Can some tell me how to do this? I am considering a
    FormManager class,
    > which
    > is a singleton class that each form can registers with
    and can use to
    > toggle
    > the visibility of itself and its siblings, but I wanted
    to see if there
    > was an
    > easier way to access methods on the class instance
    associated a parent's
    > form.
    >
    > Any suggestions?
    >
    Why don't you explicitly instantiate the children with a
    reference to the
    parent form. That way you could store the reference in the
    constructor. As
    long at the functions are public I think it should work.
    Amy

  • Parent/child classes vs badi or something else

    Dear Experts,
    I’m looking for some architectural suggestions here on the best way to implement “user exits” with classes.
    Up to now, I have coded my software similar to the SAPMV45A method of having an include with subroutines USEREXIT_BEFORE_HEADER and the like.
    It works okay except that the code doesn’t do a great job of encapsulation.  All data has to be global and that gets to be a pain for cleanup.
    I’m wondering the best way to do this as I am not an expert at all in object oriented development.  I've only implemented BADI on a couple of occasions, found it confusing and were it not for tutorials, I wouldn't have been able to do it at all.
    For example, a common flow of my RFC goes like this:
    Perform userexit_initialize in program (sy-repid) if found.
    Perform initialize.
    Perform read_header
    Perform read_items
    Perform populate_header_output
    Perform populate_item_output
    In each of the FORM routines, I have a userexit at the end to allow custom filters or database reads as necessary.
    For example:
    Form read_header
      Select * from header_tab ….
      PERFORM userexit_read_header_finished in program (sy-repid) if found.
    Endform.
    I had hoped I could have two classes, a parent or “core” class to which I can implement only child methods as needed.
    Therefore, it would be more along the lines of:
    Function z_my_rfc
      Importing i_docnum
      Exporting e_header
                     Et_items
                    Et_bapiret2
    Call zcl_core->read_header
    Call zcl_core->read_items
    Call zcl_core->populate_header
    Call zcl_core->populate_items
    What I haven’t really been able to figure out is if I can create and distribute another class zcl_child which I can then customize at every implementation by simply adding a new method and using “super” to incorporate the logic of the zcl_core class.
    I think I’ve seen that this can be done in local class development ( in an include abap program ) but I want to do this in actual classes via SE24.
    Now, my other thought is that this is what BADI are for??
    Periodically, I want to send my customers a transport with update ZCL_CORE class code/methods, etc. but I want to leave the ZCL_CHILD class alone.
    Hopefully, I’ve conveyed my desire here and perhaps you can suggest the simplest way to support this software going forward.
    Thanks in advance for your time and consideration.

    Hi Ray,
    Go for custom BADIs in this case. In fact, that is exactly the functionality you are looking for !
    Just go through this excellent five part blog on Enhancement framework by Thomas Weiss.
    What the New Enhancement Framework Is For – Its Basic Structure and Elements For Beginners
    The new Enhancement Framework Part 2 - What Else You Need to Know Before Building an Enhancement
    How To Define a New BAdI Within the Enhancement Framework - Part 3 of the Series
    How to implement a BAdI And How to Use a Filter - Part 4 of the Series on the New Enhancement Framework
    Source Code Enhancements - Part 5 of the Series on the New Enhancement Framework
    Cheers,
    Mahesh

  • How to reference an inner class with @link?

    Hi,
    Let's assume we have a class Test with an inner class namned InnerTest. If I want to put a @link to a method in InnerTest in another java-file, what do I write?
    To reference a method in Test, I write:
    {@link com.company.Test#method}
    My first thought when referencing the inner class' method was to just write:
    {@link com.company.Test.InnerTest#method}
    but this doesn't seem to be the correct way.
    Any help is greatly appreciated! Thank you in advance.

    You're calling the method in the inner class the right way.
    You're probably experiencing bugs with links.
    I don't know what version you're using, but 1.4.0 and
    1.4.1 have some severe bugs. Please see another
    thread in this forum entitled:
    "Some {@link} tags do not seem to generate hyperlinks"
    -Doug Kramer
    Javadoc team

  • How to reference /inherit property class in form in oracle apps

    Hi
    I had a Form which is register in the oracle apps but now i have another requirement to
    create a tab canvas put all fields of pervoius form(content form) into new tab page canvas
    now i want to know how i can reference/inherit the pervious conent form property class and object to new tab canvas
    since i didn't create with template fom .but i have property class in form module
    is it possible to inherit property classes
    Iam using form 6i,11i apps
    please help me

    It is possible.

  • Vectors - Saving Reference to a Class and calling it's Methods ?

    Hi,
    How can I do this ? trying to execute a method from a class reference stored in a Vector.... something like this...
    Vector v = new Vector();
    v.addElement( new MyJPanel1());
    v.addElement( new MyJPanel2());
    for (int i=0; i<v.size();i++) {
    // How can i execute the methods contained within say MyPanel1()
    // from the vector c ?
    // ie) MyPanel1().closwWindow();
    }Thanks
    Rob

    You have to get the element and cast it to the appropriate type. Then do what you want.
    JPanel panel = (JPanel) v.elementAt(i);And then there is generics but from you have posted you don't seem to have those...

  • Reference type in class

    Hi All,
        I am developing report using class.In alv grid  the reference type is like this
       data ref1 type ref to cl_gui_alv_grid
    Now I want for alv list.. what reference type I have use.
    If i Use data ref1 type ref to cl_gui_alv_list it gives error
    please help.
    Thanks,
    Rakesh

    Hi Rakesh,
    If you want your output into list format, just have a look into the following thread:
    Alv using class In list format
    Hope this will help.
    Regards,
    Nitin.

Maybe you are looking for

  • My purchased TV shows are on Apple TV, but my purchased Movies and Music is not, what gives?

    My purchased TV shows are visible on Apple TV, but my purchased Movies and Music are not, what gives? From Apple TV menu, I used to click on "Computer" and choose my laptop and I could watch TV, Movies, Photos, Music, whatever.... but now the "Comput

  • Itens of legend in the same line

    Hi, I built a chart and now I need to put the itens of legend in the same line, but the each iten is in a new line. How I can modify this? The code XML of legend <.legend enabled="yes" x="80" y="0"> <.names enabled="yes"> <.font type="Verdana" size="

  • Date formating in the Report

    Hi all, I have print one field in the report. The field actually concatenation on string and Date EX:  Selected on 30.05.2007 I have declared this field has char (30). This Date will be taking how ever the stored in the Data Base. Can anybody give me

  • Starring images in survey

    Hi Everyone - not sure if this has been covered but the search field here doesn't work (put in white balance, and nothing comes up!) so apologies if it has! Anyway: i select a few images, goto survey or lupe, and then click '[' to increase the star r

  • Installing client in linux.

    How to install the client in debian ?