In C++, it is always possible to have one class nested inside other or
for that matter, a data structure nested inside other data structure.
Something like:
class A
{
public:
struct A1
{
char a[10];
char b[10];
} a1;
struct A2
{
char a2[10];
char b2[10];
} a2;
...........
};
What i am struggling with is, i am searching for similar representation
for a java class.
I tried doing it as:
public class Temp
{
class A1
{
public String a1;
public String b1;
};
class A2
{
public String a2;
public String b2;
};
..............
};
This gets compiled but what i see is n no. of .java files get generated
named:
Temp.java
Temp$A1.java
Temp$A2.java
This behavior is coming as a surprise package to me.
I agree i dont't know much of java and am a begineer in this.
Manish Pandit - 14 Sep 2006 02:37 GMT
What you ended up is 'inner classes'. Here is something worth a read:
http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
-cheers,
Manish
> In C++, it is always possible to have one class nested inside other or
> for that matter, a data structure nested inside other data structure.
[quoted text clipped - 46 lines]
> This behavior is coming as a surprise package to me.
> I agree i dont't know much of java and am a begineer in this.
Arne Vajhøj - 14 Sep 2006 02:40 GMT
> In C++, it is always possible to have one class nested inside other or
> for that matter, a data structure nested inside other data structure.
[quoted text clipped - 46 lines]
> This behavior is coming as a surprise package to me.
> I agree i dont't know much of java and am a begineer in this.
I think you mean that the compiler generates:
Temp.class
Temp$A1.class
Temp$A2.class
1) Since Temp2 also can have an A1 and A2, then that naming
convention actually makes sense.
2) You should not worry too much about the class files generated - it
will work when you run your code.
3) Seriously I think you should consider your usage of nested
classes.
It is a rare used feature in Java.
Use packages to structure your code.
Arne
Mark Space - 14 Sep 2006 21:52 GMT
> This gets compiled but what i see is n no. of .java files get generated
> named:
[quoted text clipped - 5 lines]
> This behavior is coming as a surprise package to me.
> I agree i dont't know much of java and am a begineer in this.
Don't be concerned about this. It's just an artifact of the compiler.
This class would still be accessed as:
Temp t = new Temp();
Temp.A1 a1 = t.new A1();
Temp.A2 a2 = t.new A2();
The $ are just there for the compiler, not you. Just like C likes to
put _ in front of external variable and function names, but the user
should never see it. Java just uses $ instead of . is all.
The link Mannish posted above is excellent, please read all three
tutorial pages there.
Don't worry, be happy. ^_^