Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Nesting classes

Thread view: 
Tanveer - 14 Sep 2006 02:27 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.
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. ^_^


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.