• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Static Inner Class Challenge  RSS feed

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
private static int counter;
public static int getCounter(){return counter++;}
private static int innerCounter;
public static int getInnerCounter(){return innerCounter++;}
private String name;
A() {name = "A" + getCounter();}
static class B {
private String name;
B() {
name = "B" + getInnerCounter();
System.out.print( name);
}}
void m1() {new A().new B();} // 1
public static void main(String[] args) {
A a1 = new A();
a1.m1();
}}

This class is giving compiletion error at line one , can any body explain why ?
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

Qualified class instance creation expressions begin with a Primary. A qualified class instance creation expression enables the creation of instances of inner member classes and their anonymous subclasses.
 
psrinivas Qjava
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Keith,

Thanks for the reply, Could u plz be a bit illustrative...
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that a static nested class is not an inner class.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to be more specific a static inner class cannot be called as given in //line 1 it should be called as new B() from the enclosed class.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!