ClassInstanceCreation

D

Dimitri Kurashvili

Below is a definition of ClassInstanceCreation as it is defined in
Eclipse JDT.
I'm confused about what is a first expression (i mark it with question
sign).

========================================
ClassInstanceCreation:
[ Expression . ] < - - - - - - - - - - - - ?
new [ < Type { , Type } > ]
Type ( [ Expression { , Expression } ] )
[ AnonymousClassDeclaration ]
========================================

So i can write something like this:

========================================
SomeExpression.new String("mystring")
========================================

what may be SomeExpression that the code above can compile?

==
Thanks, Dimitri
 
S

Simon

Dimitri said:
So i can write something like this:

========================================
SomeExpression.new String("mystring")
========================================

what may be SomeExpression that the code above can compile?

This is in fact a rarely used construction. If a class A declares an inner class
I (that is not static), and you have an object a of type A, you can in fact write

A.I i = a.new I();

This is necessary, since the instance of the inner class needs a reference to an
instance of the enclosing class A.

Cheers,
Simon
 
T

Tom Hawtin

Dimitri said:
ClassInstanceCreation:
[ Expression . ] < - - - - - - - - - - - - ?
new [ < Type { , Type } > ]
Type ( [ Expression { , Expression } ] )
[ AnonymousClassDeclaration ]
SomeExpression.new String("mystring")
========================================

what may be SomeExpression that the code above can compile?

The syntax is for specifying the outer class instance when constructing
an inner class. It's exceptionally rare to require anything other than
the implicit this (if you call the constructor from an inner class, it
may be an outer this rather than the inner most this). I would stay well
away from attempting to use that particular feature.

Tom Hawtin
 
D

Dimitri Kurashvili

this is why i could not instantinate inner classes without declaring
them static!
 
L

Lew

Please do not top-post.

Dimitri said:
this is why i could not instantinate inner classes without declaring
them static!

If you declare them static, they aren't inner classes.

-- Lew
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top