names from Class.getName()

Discussion in 'Java' started by Rico, Nov 28, 2003.

  1. Rico

    Rico Guest

    The following is my attempt at a sample program to illustrate my situation
    but... well... things turn out to work fine here. that's not good! :S
    --------------------------------------------------------------------------
    class A
    {
    A()
    {
    b_here = new B();
    }

    private B b_here;

    public B getB()
    {
    return b_here;
    }
    }

    class B {}

    class C
    {
    C()
    {
    a_main = new A();
    }

    private class D
    {
    D()
    {
    System.out.println(a_main.getB().getClass().getName());
    }
    }

    A a_main;
    public static void main(String [] args)
    {
    a_main = new A();
    new C().new D();
    }
    }
    -------------------------------------------------------------------------
    In my project what I'm observing is that from class D's constructor what's
    printed out is not "B" but something like "A$1"...
    Anyone with some idea as to why? Thanks.

    Rico.
     
    Rico, Nov 28, 2003
    #1
    1. Advertising

  2. Rico wrote:
    > The following is my attempt at a sample program to illustrate my situation
    > but... well... things turn out to work fine here. that's not good! :S
    > --------------------------------------------------------------------------
    > class B {}
    > [CUT]
    > class C
    > {
    > C()
    > {
    > a_main = new A();
    > }
    >
    > private class D
    > {
    > D()
    > {
    > System.out.println(a_main.getB().getClass().getName());
    > }
    > }
    >
    > A a_main;
    > public static void main(String [] args)
    > {
    > a_main = new A();


    You cannot access non-static a_main from a static context.

    > new C().new D();
    > }
    > }
    > -------------------------------------------------------------------------
    > In my project what I'm observing is that from class D's constructor what's
    > printed out is not "B" but something like "A$1"...
    > Anyone with some idea as to why? Thanks.


    I've compiled and run, and prints "B".

    --
    /**
    * Mattia Belletti - Undergraduate student @ cs.unibo.it
    * ICQ: 33292311 - email:
    * IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
    * Linux registered user 299762 @ Linux registered machine 213003
    */
     
    Mattia Belletti, Nov 28, 2003
    #2
    1. Advertising

  3. Rico

    Joana Guest

    hello,

    > A a_main;
    > public static void main(String [] args)
    > {
    > a_main = new A();

    this is wrong.Compiler will show an error! Because a_main isn't static
    and main-function is static.

    This :
    public static void main(String [] args)
    {
    // a_main = new A();
    new C().new D();
    }

    gives output B.

    regards,
    joana




    > -------------------------------------------------------------------------
    > In my project what I'm observing is that from class D's constructor what's
    > printed out is not "B" but something like "A$1"...
    > Anyone with some idea as to why? Thanks.
    >
    > Rico.
     
    Joana, Nov 28, 2003
    #3
  4. Rico

    Juha Laiho Guest

    Rico <> said:
    >The following is my attempt at a sample program to illustrate my situation
    >but... well... things turn out to work fine here. that's not good! :S

    ....
    >In my project what I'm observing is that from class D's constructor what's
    >printed out is not "B" but something like "A$1"...
    >Anyone with some idea as to why? Thanks.


    Looks like ..hmm.. static inner class. Is in your project 'B' a static
    class contained within 'A'? If so, it becomes to a toplevel class with
    name 'A$[number]'.
    --
    Wolf a.k.a. Juha Laiho Espoo, Finland
    (GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
    PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
    "...cancel my subscription to the resurrection!" (Jim Morrison)
     
    Juha Laiho, Nov 30, 2003
    #4
  5. Rico

    Chris Smith Guest

    Juha Laiho wrote:
    > Rico <> said:
    > >The following is my attempt at a sample program to illustrate my situation
    > >but... well... things turn out to work fine here. that's not good! :S

    > ...
    > >In my project what I'm observing is that from class D's constructor what's
    > >printed out is not "B" but something like "A$1"...
    > >Anyone with some idea as to why? Thanks.

    >
    > Looks like ..hmm.. static inner class. Is in your project 'B' a static
    > class contained within 'A'? If so, it becomes to a toplevel class with
    > name 'A$[number]'.


    That would be accurate for anonymous inner classes. Static nested
    classes would be given names built from the actual name of the nested
    class as well as the containing class. As for "static inner classes",
    no such thing exists; the static keyword is used to prevent a nested
    class from being an inner class.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Dec 1, 2003
    #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. Tom
    Replies:
    1
    Views:
    8,352
    Paul Tomblin
    Jun 27, 2003
  2. Rico
    Replies:
    4
    Views:
    429
    Michael Borgwardt
    Dec 2, 2003
  3. Robert Mark Bram

    class.getName()

    Robert Mark Bram, Jun 17, 2004, in forum: Java
    Replies:
    8
    Views:
    4,136
    Jeff Robertson
    Jun 18, 2004
  4. Replies:
    1
    Views:
    464
    John C. Bollinger
    May 2, 2005
  5. WT
    Replies:
    6
    Views:
    943
    Walter Wang [MSFT]
    May 18, 2006
Loading...

Share This Page