names from Class.getName()

R

Rico

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.
 
M

Mattia Belletti

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
--------------------------------------------------------------------------
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();
}
}

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

--
/**
* Mattia Belletti - Undergraduate student @ cs.unibo.it
* ICQ: 33292311 - email: (e-mail address removed)
* IRC: BluShine - site(s): http://cs.unibo.it/~mbellett
* Linux registered user 299762 @ Linux registered machine 213003
*/
 
J

Joana

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
 
J

Juha Laiho

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]'.
 
C

Chris Smith

Juha said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top