• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Instantiation of static member class

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
This is with regard to the instantiation of static member classes. I know that to instantiate a static member class, we do not need an instance of the enclosing class. However I thought that instantiating through an instance of the enclosing class was also fine. Hence I tried the following code.

However I got the compile time error "qualified new of the static class" on lines 1 and 2. Line 3 had no such problem since I used the normal syntax for instantiating a static member class.
Since static members of a class can be accessed with / without an instance of the class, I thought the same is applicable to the instantiation of the static member class. However it was not so..Can anyone please provide inputs. My last question on Exceptions is still pending and I am quite apprehensive abt this one ..Hope it's not so
Thanks all
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sirish. I can show you the rules. But I cannot say why the rules are what they are.
According to JLS 15.9
ClassInstanceCreationExpression:
new ClassOrInterfaceType ( ArgumentListopt ) ClassBodyopt
Primary.new Identifier ( ArgumentListopt ) ClassBodyopt
ExpressionName.new Identifier ( ArgumentListopt ) ClassBodyopt
----
Case 1: An unqualified class instance creation expression
new ClassOrInterfaceType ( ArgumentListopt ) ClassBodyopt
new Outer.Inner()
new Inner()
ClassOrInterfaceType is a type name, such as Outer, Inner or Outer.Inner
----
Case 2: A qualified class instance creation expression
Primary.new Identifier ( ArgumentListopt ) ClassBodyopt
ExpressionName.new Identifier ( ArgumentListopt ) ClassBodyopt
new Outer().new Inner()
m().new Inner()
out.new Inner()
Identifier is the simple name of an inner class that is a member of the type of Primary or ExpressionName.
----
1. Outer.Inner i = out.new Inner();//line 1
out.new Inner() has the form
ExpressionName.new Identifier ( ArgumentListopt ) ClassBodyopt
Therefore, Inner must be the simple name of an inner class of Outer.
2. Outer.Inner j = new Outer().new Inner();//line 2
new Outer().new Inner() has the form
Primary.new Identifier ( ArgumentListopt ) ClassBodyopt
Therefore, Inner must be the simple name of an inner class of Outer.
3. Outer.Inner k = new Outer.Inner();//line 3
new Outer.Inner() has the form
new ClassOrInterfaceType ( ArgumentListopt ) ClassBodyopt
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry, am lost in the explanation that Marlene gave. Can someone please make it simpler?
Guru.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guru. Can I try again?

Here are some ways to instantiate a non-static member class B.

Here are some ways to instantiate a static member class C.

We cannot say the following.

Why not? because the JLS says we must not.

[ February 03, 2004: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my own way of thinking about this subject. However, I cannot back this up with the JLS. It�s just the way I have organized these ideas in my head.
We start by specifying a fully qualified name. You cannot go wrong (usually) with a fully qualified name.
new A.B()
new A.C()
But if you are inside the class A, you don�t have to qualify the name, although you can if you want to (usually).
new B()
new C()
However, don�t forget that an instance of a non-static member class B has a reference to an instance of the enclosing class A.
So when you are in a static context, you must specify the instance of the enclosing class. Or if you want to specify some instance other than the current object �this�.
new A().new B()
a.new B()
You cannot do the following, I suppose because you would be specifying two type names for the enclosing class.
new A().new A.B() // error
a.new A.B() // error
And you cannot do the following, I can only guess because the outer object is not needed, it serves no purpose.
new A().new C() //error, static member class
a.new C() // error static member class
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it couldn't be more clear to me. DId you follow Guru?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Andres and Guru. It helps me to know when I am making sense and when I am not.
[ February 04, 2004: Message edited by: Marlene Miller ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic