Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Inner Classes

 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could someone tell me why line 2 and 3 does not compile? Thanks....

class cn12
{
static class j
{
static class iop
{
class iopgf
{
public void test()
{
System.out.println ("I am here"); }
}
}
}

public static void main(String args[])
{
cn12.j.iop.iopgf y=new cn12.j.iop().new iopgf(); //Line 1 NOT WORKING
cn12.j.iop.iopgf y=new cn12().new j().new iop().new iopgf();// Line 2 NOT WORKING
cn12.j.iop.iopgf y = new cn12.j.new iop().new iopgf();// Line 3 NOT WORKING
y.test();
}
}
(Oops - I hit the edit icon rather than the quote icon - Sorry. MdQ)
[ April 17, 2002: Message edited by: Marilyn deQueiroz ]
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Line 2 works if you comment out Line 1 and Line 3. You cannot define y more than once.

Line 3 works if you comment out Line 1 and Line 2 and also add parentheses after the 'j'.
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I forgot to mention that either two of the previous three lines were to be commented out before they are compiled.
However, Marilyn, line 2 nor line 3 could compile when I execute either one of them.
//cn12.j.iop.iopgf y = new cn12.j.iop().new iopgf(); // Line 1 - Only this works
cn12.j.iop.iopgf y = new cn12().new j().new iop().new iopgf();// Line 2
//cn12.j.iop.iopgf y = new cn12.j().new iop().new iopgf(); // Line 3
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've just tried to figure it out by asking my compiler. The results were really strange

Line 1 (with lines 2 and 3 commented out) works fine.

However, I don't understand why the brackets behind iop() are necessary. Maybe someone can help me to understand.
Line 2 (with lines 1 and 3 commented out) throw a compile-time error, saying that classes cn12.j and cn12.j.iop are not inner classes (I remember having read some more details about static inner classes in the campfire section, Get in touch with your inner class).
And line 3 (with lines 1 and 2 commented out) doesn't compile either, unless you delete the "new" before iop.
[ April 17, 2002: Message edited by: Mag Hoehme ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm. Worked fine for me using jdk1.3.1 on Win2K
Which jdk are you using?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of it this way:
Object o = new Object();

Now substitute (cn12.j.iop) for Object in the above statement. You get:
cn12.j.iop y = new cn12.j.iop();

See?
 
Mag Hoehme
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm creating a new instance of the (static) class cn12.j.iop.
And I do need this instance for creating an instance of an inner class (new Outer().new Inner()).
By the way, I'm using jdk1.4.0 in win2k.
Please let me know if I'm wrong. Thank you very much for your help.
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Line2 will work only if class j and class iop are not declared static.
I looked at it at two parts: -
1. new cn12.j.iop() - static classes does not require an instance of its enclosing classes
2. new iopgf(); - since this class is not static, thus it would need the instances of its enclosing classes, coming from (1), thus we have:
new cn12.j.iop().new iopgf();

Hope my understanding is correct.
Clement
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, I bliv there's no issue about the JDK version for this question...
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Clement Ng:
Also, I bliv there's no issue about the JDK version for this question...


I think there is version problem as it compiles fine with jsdk1.2 and jsdk1.3. Here is the output when I compile it with above versions.
===========================
D:\JavaTp\inner>javac1.2 Outer.java
D:\JavaTp\inner>C:\jdk1.2.2\bin\javac.exe Outer.java
D:\JavaTp\inner>javac1.3 Outer.java
D:\JavaTp\inner>C:\jdk1.3.1_01\bin\javac.exe Outer.java
D:\JavaTp\inner>javac1.4 Outer.java
D:\JavaTp\inner>C:\j2sdk1.4.0\bin\javac.exe Outer.java
Outer.java:9: qualified new of static class
Outer.Nested nested = new Outer().new Nested() ;
^
1 error
D:\JavaTp\inner>
===========================

here is my Outer.java code:
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Clement Ng:
cn12.j.iop.iopgf y=new cn12.j.iop().new iopgf(); //Line 1 NOT WORKING
cn12.j.iop.iopgf y=new cn12().new j().new iop().new iopgf();// Line 2 NOT WORKING
cn12.j.iop.iopgf y = new cn12.j.new iop().new iopgf();// Line 3 NOT WORKING


Hi Clement
Line 1 will work.
Line 2 will work.
Line 3 will not work for a reason the very first "new" is unused. I mean it is not creating any object.
And your understanding is very much correct.
All the best
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it is a bug with bug ID: 4670161
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic