How to access the method in such a inner class?

Discussion in 'Java' started by Bruce Sam, Jan 12, 2005.

  1. Bruce Sam

    Bruce Sam Guest

    /* ************************* */
    public class A {
    public void g() {
    class B {
    public void f() {
    System.out.println("f()");
    }
    }
    }
    public static void main(String[] args) {
    }
    }
    /* ************************** */
    Above program is an incomplete code.The code I want to add is that
    access the meothod of f();But I don't know how to access the method f()
    in such a inner class?
     
    Bruce Sam, Jan 12, 2005
    #1
    1. Advertising

  2. On 12 Jan 2005 00:35:23 -0800, Bruce Sam wrote:

    ....
    > public class A {
    > public void g() {


    Please indent any code posted.

    ....
    > Above program is an incomplete code.The code ...


    Putting ' ' after a full stop (.) would help as well. Otherwise
    sentences run together. It is harder to read them.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jan 12, 2005
    #2
    1. Advertising

  3. Bruce Sam

    John English Guest

    Bruce Sam wrote:

    > /* ************************* */
    > public class A {
    > public void g() {
    > class B {
    > public void f() {
    > System.out.println("f()");
    > }
    > }
    > }
    > public static void main(String[] args) {
    > }
    > }
    > /* ************************** */
    > Above program is an incomplete code.The code I want to add is that
    > access the meothod of f();But I don't know how to access the method f()
    > in such a inner class?


    In g():
    B b = new B();
    b.f();

    -----------------------------------------------------------------
    John English | mailto:
    Senior Lecturer | http://www.it.bton.ac.uk/staff/je
    School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
    University of Brighton | -- see http://burks.bton.ac.uk
    -----------------------------------------------------------------
     
    John English, Jan 12, 2005
    #3
  4. Bruce Sam

    Bruce Sam Guest

    Hello John,It's not clear that what I said before,I want to access the
    method f( ) in the main( ).
     
    Bruce Sam, Jan 12, 2005
    #4
  5. Bruce Sam wrote:

    > Hello John,It's not clear that what I said before,I want to access the
    > method f( ) in the main( ).


    Put this code, based on what John English gave you, into main():

    B b = (new A()).new B();
    b.f();

    Or do exactly as John suggested and also put this in main():

    A a = new A();
    a.g();

    The main method is static; you cannot invoke instance methods without an
    instance to invoke it on. Furthermore, B is an inner class of A; you
    cannot have an instance of B without a containing instance of A. An
    alternative, then, would be to make B a static nested class and make f()
    a static method of B, in which case you could simply do this from main():

    B.f();

    Unless this is purely hypothetical, program design considerations will
    govern which approach is most appropriate, and indeed, whether invoking
    B.f from A.main() is appropriate at all.


    John Bollinger
     
    John C. Bollinger, Jan 12, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kishan bisht
    Replies:
    2
    Views:
    2,944
    xarax
    Jul 2, 2003
  2. Yamin
    Replies:
    4
    Views:
    16,806
    Yamin
    Oct 24, 2004
  3. E11
    Replies:
    1
    Views:
    4,887
    Thomas Weidenfeller
    Oct 12, 2005
  4. Carlo v. Dango
    Replies:
    14
    Views:
    1,085
    Alex Martelli
    Oct 19, 2003
  5. Pyenos
    Replies:
    2
    Views:
    406
    Pyenos
    Dec 27, 2006
Loading...

Share This Page