Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we have public inner class

 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TestClass {
public class A {
public void m() { }
}
class B extends A {
}
public static void main(String args[]) {
new TestClass().new A() { public void m() { } };
}
}

this code compiles fine???but we have two public class A which is inside public class testClass ...is this okie
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can have a public inner class inside a public class. The code will compile and run fine...
 
Arijit Daripa
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mukki pandey:
public class TestClass {
public class A {
public void m() { }
}
class B extends A {
}
public static void main(String args[]) {
new TestClass().new A() { public void m() { } };
}
}


this code compiles fine???but we have two public class A which is inside public class testClass ...is this okie



You can append public, private, protected, abstract, final(but not final and abstract both), strictfp to the inner-class-name. You can also append static, but then the inner class will become static nested class( I think that is the name for that).
[ September 28, 2008: Message edited by: Arijit Daripa ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic