Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner classes

 
y sudhakar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai all,
I wrote one class and within that I wrote a inner class. If I make some of the variable in the inner class static the compiler is complaing that inner class should not contain static member.
I am not able to understand why it should not be like that.
Is there any body who can explain what is the exact meaning behind this.
Thanx
sudhakar
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
unless inner class is not static inner class you are not allowed to have static members in it. There is one thing you can do to have a member static is by making it final but remember it's not the same then because it's a constant.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudhakar,
The reason that constraint is placed on inner classes is this :
Unless an inner class is specified as static (which means it always exists), a non-static inner class needs an instance of the outer class to exist... Static attributes always exist, so how could a static attribute always exist if the class containing it ( the non-static inner class ) does not exist because it has no outer class to contain it?
HTH,
-Nate
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y,
Please change your name to be compliant with JavaRanch's naming policy.
Your ID should be 2 separate names with more than 1 letter each. We really want this to be a proffesional forum and would prefer that you use your REAL name.
Thanks,
Cindy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic