Nesting classes

T

Tanveer

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

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Tanveer said:
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.

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
 
M

Mark Space

Tanveer said:
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.

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. ^_^
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top