Reference outer class explicitly

Hi, I have this code:
public class Outer {
  public class Inner {
    public String getName() {
      return "inner";
    public String getOuterName() {
      return ""; // ????????????????????????
  public String getName() {
    return "outer";
}Where I have the question marks I want to know how to explicitly call the getName method for the enclosing class of the Inner class instance. What I've always done is make a new method in Outer called "doGetName" and then I can reference it from the inner class since the inner class doesn't have a method by the same name. But that solution is clumsy. I know that all inner classes have references to their outer class...
Anyway, any help would be much appreciated!

try
return Outer.this.getName();

Similar Messages

  • How to reference a class without using the new keyword

    I need to access some information from a class using the getter, but I don't want to use the new keyword because it will erase the information in the class. How can I reference the class without using the new keyword.
    ContactInfo c = new ContactInfo(); // problem is here because it erases the info in the class
    c.getFirstName();

    quedogf94 wrote:
    I need to access some information from a class using the getter, but I don't want to use the new keyword because it will erase the information in the class. How can I reference the class without using the new keyword.
    ContactInfo c = new ContactInfo(); // problem is here because it erases the info in the class
    c.getFirstName();No.
    Using new does not erase anything. There's nothing to erase. It's brand new. It creates a new instance, and whatever that constructor puts in there, is there. If you then change the contents of that instance, and you want to see them, you have to have maintained a reference to it somewhere, and access that instance's state through that reference.
    As already stated, you seem to be confused between class and instance, at the very least.
    Run this. Study the output carefully. Make sure you understand why you see what you do. Then, if you're still confused, try to rephrase your question in a way that makes some sense based on what you've observed.
    (And not that accessing a class (static) member through a reference, like foo1.getNumFoos() is syntactically legal, but is bad form, since it looks like you're accessing an instance (non-static) member. I do it here just for demonstration purposes.)
    public class Foo {
      private static int numFoos; // class variable
      private int x; // instance varaible
      public Foo(int x) {
        this.x = x;
        numFoos++;
      // class method
      public static int getNumFoos() {
        return numFoos;
      // instance method 
      public int getX() {
        return x;
      public static void main (String[] args) {
        System.out.println ("Foo.numFoos is " + Foo.getNumFoos ());
        System.out.println ();
        Foo foo1 = new Foo(42);
        System.out.println ("Foo.numFoos is " + Foo.getNumFoos ());
        System.out.println ("foo1.numFoos is " + foo1.getNumFoos ());
        System.out.println ("foo1.x is " + foo1.getX ());
        System.out.println ();
        Foo foo2 = new Foo(666);
        System.out.println ("Foo.numFoos is " + Foo.getNumFoos ());
        System.out.println ("foo1.numFoos is " + foo1.getNumFoos ());
        System.out.println ("foo1.x is " + foo1.getX ());
        System.out.println ("foo2.numFoos is " + foo2.getNumFoos ());
        System.out.println ("foo2.x is " + foo2.getX ());
        System.out.println ();
    }

  • Question about Inner/Outer classes

    Hello!
    I'm still preparing for java cert exam and have a question. If I have Outer and Inner classes like this:
    Outer o = new Outer();
    Outer.Inner i = o.new Inner();Does this mean that 'i' has a reference to both Outer and Inner classes. Moreover, does 'i' have a reference to the 'this' of 'o'?
    Thanks,
    Victor.

    "i" doesn't have a reference. "i" is a reference. I think you are trying to express this:
    import java.lang.reflect.*;
    public class Outer {
        class Inner {
            private int foo;
        public static void main(String[] args) {
            Outer o = new Outer();
            Outer.Inner i = o.new Inner();
            for(Field field : i.getClass().getDeclaredFields()) {
                System.out.format("%s %s %s;%n", Modifier.toString(field.getModifiers()),
                    ((Class)field.getGenericType()).getName(), field.getName());
    }

  • Accessing outer class memebers (objects) from inside a local class

    Why is it neccessary that any Member-Object reference of the Outer class, or rather even the Object references defined inside the method, which contains the local/anonymous class, has to be declared as "final" if the reference is tobe used inside the definition of the inner class??

    This feature of Java has often annoyed me. The Java Tutorial simply says "A nested class declared within a method or other smaller block of code has access to any final, local variables in scope", without explanation.
    I can think of a partial explanation: if inner classes could access non-final variables, this code would compile:
    void aMethod()
    int x = 5;
    // button is a member variable of this class
    button.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e)
    x = 6;
    but actionPerformed may be called long after aMethod returns and x ceases to exist, resulting in an exception.
    However, requiring that x be final doesn't solve all problems. You wouldn't be able to write x = 6 if x was final, but you would be able to write System.out.println(x), which is equally problematic.

  • Closing ADODB sub reference outs in VIs? (LV 8.5.1)

    I had a question about closing references to items I have not explicitly opened.
    e.g.
    I create an ADODB connection object.
    From that, I create an ADODB command object.
    One of the Command properties is the Parameters reference. I do a parameters.Refresh and using count, I loop through a For loop getting Item references. For each Item reference, I get a parameter object.
    One of the properties in the parameter object is a Properties reference.  I do a refresh on that and then go into another For loop. For each Item reference, I get a property node.
    OK, now that I've gone through getting/setting information, I need to start working my way out. The last property node had an item reference out from the properties node. Should I close that? The properties node itself had as a reference out, should that be closed? The Parameter node has a reference out, should that be closed? Finally, the Parameters node has a reference out, should I close that?
    The command and connection nodes I explicitly opened, so I know I need to close them. But these other references, should I be closing them, or do I only need to close the references I explicitly created?
    Thanks, Bob

    "intp56" <[email protected]> wrote in message
    news:[email protected]..
    > I download LabSQL, it looks pretty close to what I want.
    > &nbsp;
    I'd suggest to use it, and to send them your additional code in return if
    you're alowed to do that... Or suggest what you are missing to the project,
    so they can add it in later versions.
    > I'm looking for a VI that queries a stored procedure to determine what the
    parameters are, then creates a LabVIEW cluster with the correct datatypes.
    The idea is to call that first to create the Parameters object, then use it
    later, particularly for DML operations. (Say insert rows from a LV table.)
    I've tried using ADO recordsets to insert large datasets, and found it's
    performance to be lacking or unacceptable. I can insert several 1000 records
    per second with successive calls to a stored procedure where I specify ahead
    of time no result set.
    Not sure how ADO does this. I vaguely recall something about it. I think the
    values are send item by item, and I think there was a faster way to send the
    data by using a variant that directly accepts an array... It's been a while
    since I've messed around with that, and I might not have succeeded to get it
    working. Googling for "ADO large data sets" or something might give some
    clues.
    > If I really have lots of rows to insert, then I've found it better to
    prepare a file, move it to a place where the database can see it, and BULK
    INSERT it asynchronously and report the results back later.
    Can't imagine that this is the best way to do it, but it's a good
    workaround.
    > BTW, I liked the tip that any reference WIRE needs to be closed, that
    gives me a nice rule to know what I need to closed.
    But even if there is no wire comming out of a reference property, you still
    need to close it. So: every "green" read property should be closed.
    Regards,
    Wiebe.

  • Inner class access to outer class variables

    class X extends JTextField {
    int variable_a;
    public X() {
    setDocument(
    new PlainDocument() {
    //here i need access to variable_a
    i don't want to make variable_a a static member though, so does anyone know how to do it?

    Generally, you can reference your variable_a just as you would any other variable except if your inner class also defines a variable_a. However, to produce a reference to the enclosing outer class object you write "OuterClassName.this". So, to get to your variable I think you would write "X.this.variable_a". I hope this helps.

  • Getting the name of outer class in an inner class

    Hi,
    I have a private inner class, something like this:
    public class OuterClass extends AnotherClass {
    public OuterClass() {
    supre();
    private class innerClass1 extends SomeotherClass {
    protected void someMethod() {
    // how to get the name of outer class, that is, "OuterClass"
    Can someone please tell me how to get the name of the outer class inside the inner class method?
    Thanks in advance..
    Prasanna

    getClass().getEnclosingClass().getName()But then, you already know it, don't you?

  • How to reference the class-instance, created in parent class.

    Hi, I have following scenario. During compilation I am gettting error message -- "cannot resolve symbol - symbol : Variable myZ"
    CODE :
    under package A.1;
         ClassZ
         Servlet1
              ClassZ myZ = new ClassZ;
    under package A.2;
         Servlet2 extends Servlet1
              myZ.printHi();
    How to reference the class-instance created in the parent class?

    some corrections ...
    under package A.1;
         ClassZ
         Servlet1
              init()
                   ClassZ myZ = new ClassZ;
    under package A.2;
         Servlet2 extends Servlet1
              myZ.printHi();

  • The XElement class explicitly implements the IXmlISerializable interface?

    I see a sentence on a training book.
    "The XElement class explicitly implements the IXmlSerializable
    interface, which contains the GetSchema, ReadXml, and
    WriteXml methods so this object can be serialized."
    I don't understand it. From
    the example, explicitly implements the IXmlSerializable interface is from a custom class rather than XElement class. Is
    MSDN wrong?

    I mean in the stackoverflow example,the class is a customized one "MyCalendar:IXmlSerializable"
    public class MyCalendar : IXmlSerializable
    private string _name;
    private bool _enabled;
    private Color _color;
    private List<MyEvent> _events = new List<MyEvent>();
    public XmlSchema GetSchema() { return null; }
    public void ReadXml(XmlReader reader)
    If XElement is a class that implements IXmlSerializable, what is the detail then?
    public class XElement : IXmlSerializable
    public XmlSchema GetSchema() { *detail*?; }
    public void ReadXml(XmlReader reader){*detail*?}
    Any source code of XElement class?

  • Hw To Make Inner Class Distinguish Func Parameter And Outer Class Var

    i fail to make an inner class distinguish among function parameter and outer class variable.
    consider the following code:
    public class a {
         int var; // i want to access this one using inner class.
         public a(int var) {
              this.var = var;
              class b {
                   public void fun() {
                        var = 100;
    i get an error a.java:9: local variable var is accessed from within inner class; needs to be declared final.
    when i change the code to this:
    public class a {
         int var; // i want to access this one using inner class.
         public a() {          
              class b {
                   public void fun() {
                        var = 100;
    it compiled no problem.it seems that inner class is accessing function parameter rather than outer class member.
    is there any syntax i can use to make me access the outer class member variable whithout renaming or removing the function parameter?
    thank you.

    a.this.var = 100; //Use this in the inner class.Amazing syntax -:)

  • Can I reference static class vars via a string?

    Hi all,
    I am trying to set the value of some static vars but I need to reference the class with a string rather than a proper constructor. In AS3 I can do this with:
    var ref:Class = getDefinitionByName(classDefinitionAsString) as Class;
    ref[varNameAsString] = newValue;
    I can't find an equivelant approach that will work in AS2 though. I have tried:
    var ref:Object = new [classDefinitionAsString];
    ref[varNameAsString] = newValue;
    This seems to just create an Object and as the vars that I am trying to access are static I don't really want to be using 'new' I just want to reference the vars.
    Is this even possible in AS2 and if so can anyone explain how I might do this?
    Cheers for any help

    I seem to have found a solution that works (for now at least). Not quite sure of the details
    but this does it for me.
    http://dynamicflash.com/2005/03/class-finder/

  • Outer Class Accessing Inner Classes Variables

    Hi Everyone,
    Should an outer class directly access the private member variables of its inner class? Or should it get their values by calling an appropriate 'getXXX()' method?
    Just wondering.
    Thx.

    If the outer class trys to access the variable that is declared in the inner class with in a class and outside the method, then it can access in the following example
    class outer
         private int x=10;
         class inner
              int y=20;
         public void getOutput()
              inner in=new inner();
              System.out.println("The value of y is" +in.y);
         public static void main(String args[])
              outer out=new outer();
              out.getOutput();
    };

  • Getting my hands on var/methods of outer classes

    Hello,
    I am learning java these days (at school) and i ran into a problem having an action listener as a inner class, how do i get access to methods and variables of the outer class from the inner class ??
    for instance, this is my Outer class's constructor:
         public GBLEditor() {
              super("GridBagLayout Editor");
              ToolBar toolBar = new ToolBar();
              setJMenuBar(new MainMenuBar());
              getContentPane().add(toolBar, "North");
              Table tbl = new Table();
              getContentPane().add(new JScrollPane(tbl), "Center");
              getContentPane().add(new StatusPanel(), "South");
              MenuToolBarActionListener actionListener = new MenuToolBarActionListener();
              toolBar.getNewButton().addActionListener(actionListener);
              toolBar.getOpenButton().addActionListener(actionListener);
              toolBar.getSaveButton().addActionListener(actionListener);
              toolBar.getGenerateButton().addActionListener(actionListener);
              toolBar.getNewRowButton().addActionListener(actionListener);
              toolBar.getUpRowButton().addActionListener(actionListener);
              toolBar.getDownRowButton().addActionListener(actionListener);
              toolBar.getHelpButton().addActionListener(actionListener);
              setSize(770,300);
              setVisible(true);
    So if the button newRowButton is pressed in my toolbar, i need to access the tbl object's method getTableModel() from inside the inner class below (as far as i can see now)
    And this is my inner class:
    class MenuToolBarActionListener implements ActionListener {
              public void actionPerformed(ActionEvent e) {
    any ideas?

    Now your toolBar is local constructor's variable.
    Just declare it outside the constructor and you'll have access to it from methods of inner class.
    Something like this:
    public class GBLEditor extends ... {
        private Table tbl;
        public GBLEditor() {
            tbl = new Table();
        class MenuToolBarActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
               tbl.getTableModel();
    }

  • How do I reference a class in a .dll library?

    How do I reference a class in a .dll library?  The following code extracts the contents of a ListView after a line is MouseDoubleClick:
        static public void GetListViewValues(object sender, List<string> liststListViewValues)
          ListViewItem lvi = sender as ListViewItem;
          ListViewTabs obj = lvi.DataContext as ListViewTabs;
          liststListViewValues.Add(obj.tabNumber);
          liststListViewValues.Add(obj.tabDetails);
        public class ListViewTabs
          public string tabNumber { get; set; }
          public string tabDetails { get; set; }
    I want to move this code (not the class) into a library .dll.  The .dll is generic ... could be called by any namespace.  It needs to know about the ListViewTabs class in this example.
    bhs67

    >>How do I reference a class in a .dll library?
    You add a reference to the .dll (https://msdn.microsoft.com/en-us/library/wkze6zky.aspx?f=255&MSPPError=-2147217396) and add a using statement for the namespace
    in which the class is defined at the top of the code file in which you want to use the class:
    using YourNameSpace;
    >>I want to move this code (not the class) into a .dll.
    The method must be put into some class. You could create a class in the class library and put the method in there:
    namespace YourNameSpace
    public class YourClass
    static public void GetListViewValues(object sender, List<string> liststListViewValues)
    ListViewItem lvi = sender as ListViewItem;
    ListViewTabs obj = lvi.DataContext as ListViewTabs;
    liststListViewValues.Add(obj.tabNumber);
    liststListViewValues.Add(obj.tabDetails);
    public class ListViewTabs
    public string tabNumber { get; set; }
    public string tabDetails { get; set; }
    >>The .dll is generic ... could be called by any namespace.
    Every class belongs to a namespace (potentially the global (or unnamed) namespace) in C#.
    >>It needs to know about the ListViewTabs class in this example.
    If the method in the .dll needs to know about the ListViewTabs class you must put the ListViewTabs class into the same .dll or create another .dll, put the ListViewTabs class there and then add a reference to this .dll from the one containing your method.
    You cannot keep the ListViewTabs class in the WPF application project from which you add a reference to the .dll that contains the method because you cannot add a reference from A to B and from B to A as this will lead to a circular dependency.
    Hope that helps.
    Please remember to close your threads by marking helpful posts as answer and please start a new thread if you have a new question.

  • Inner/Outer classes - curious matter

    Hi,
    This is from Sun Certified Programmer practice exam:
    class Outer {
       class Inner {}
    class ExtendedOuter extends Outer {
       class ExtendedInner extends Inner {}
    class Main extends Outer.Inner {
       Main( Outer ref ) {
            ref.super();
       public static void main( String[] args ) {
            new Main( new ExtendedOuter() );
    }The choices are:
    A) Compilation fails
    B) An exception is thrown at runtime
    C) The code runs with no output
    My answer was A because the line ref.super() doesn't make sense to me.
    But the correct answer is C.
    As far I as knew - super is a keyword and can be used either to access hidden members of the superclass, or to invoke specific constructor from the superclass also. What is it here???
    Could somebody explain what this line does?

    Is that because class files with no declared package
    location are automatically placed in the same
    package?Classes with no declared package are all part of an unnamed package, commonly referred to as the default package.
    In general, you should always use packages, as support for unnamed packages is implementation dependent (although all compilers and JVMs must support an unnamed package). Depending on the platform you are using, the unnamed package might include all directories and jar files on your classpath, just the current working directory, just the first directory on your classpath, or something else.
    But `ref' is not an instance of Main, but is an
    instance of ExtendedOuter (whose superclass is
    Object). Actually ExtendedOuter's superclass is Outer. However all that is important is that it is an instance of Outer, since the type checking is performed at compile time.
    So how can it be calling Main's?Because it is a superclass constructor invocation in class Main. Therefore it calls a constructor in Main's superclass. This is the syntax used to call a superclass constructor where the superclass is an inner class and the enclosing instance needs to be specified. ref is the object to be used as the enclosing instance - there is no clever trick to it - it is just a syntax.
    Also, if an instance of an innerclass exists, than an
    instance of it's outerclass must also exist. Yes, in the example the outer class instance is ref.
    Then
    won't calling the constructor of a superclass (with
    `ref.super()') be calling the constructor again after
    it (the superclass) has already existed?You can not create an object (other than instances of java.lang.Object) without calling a superclass constructor.

Maybe you are looking for

  • SRM, Netweaver installation on HP-UX 11i

    Hi All, In our company, we are going to implement SRM server 5.5. So, in our landscape we are going to install SRM, XI, BW and EP on a single server. My question is if in a single server we install all the applications, what about the hostname. Will

  • How to get itunes on my iphone

    I have a 5S and want to use it instead of my ipod for Nike+. I can't seem to get my itunes to work - only a couple of songs are there and can't see my playlists from itunes.

  • Flashback transaction query output

    Hi there, Here is my small demo. C:\Documents and Settings\Aman>net start oracleserviceorcl The OracleServiceORCL service is starting............................. The OracleServiceORCL service was started successfully. C:\Documents and Settings\Aman>

  • Help identifying Tecra model

    I've had a series of used Tecra's over the years, and have been very happy with that.  I am now looking at buying a used Tecra M11, but I cannot find identification of the exact model from the Part # I have been given.  I was told the part number is

  • How to install Dual OS in Mid MB pro?

    How to install Dual OS in Mid MB pro? let me know the way to back up the Mac OS in my PC.