This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt In Static Memeber...Plz Help Me  RSS feed

 
Thangaraj Selvamani
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why non-static Inner class cannot have static members but
Non-static inner classes can contain final static fields.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well thangaraj this is because non-static inner class is always associated with an enclosing class instance. So practically it's members should be inaccessible to any code directly without creating an instance of enclosing class. but if non-static inner class is allowed to have static members, then you can do this



but in case of static constants, the constant value is replaced by the compiler with the actual value of the constant



the System.out.println(Outer.Inner.i); will become System.out.println(10);

I think this is the reason, let's see what others have to say...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!