• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused regarding an inner class question  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I was seeing an static inner class example, but I did not understand whether one statement in it is correct or not. This is the code:


in this code, the Nest class usage is as expected. But I did not understand how B2 class is being instantiated. Is it possible to use new keyword to instantiate a static class?

thanks
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:Is it possible to use new keyword to instantiate a static class?

Yes it is.
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static keyword is used to identify that a particular variable is a Class variable and not an instance variable.

By declaring inner class(i.e. B2) as static, we are trying to say that it is a Class variable and its object will be shared by each and every objects of Outer class(in your case it is Broom).

Hope you will get the point.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replying. I just had my doubts about new keyword being used for these static class.. Instance creation is meaningless in this case, isn't it? other things are ok.. I understood it.
thanks
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:I just had my doubts about new keyword being used for these static class.. Instance creation is meaningless in this case, isn't it?


Why do you think it is meaningless??? Can you give some reasons for the same???
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:
s ravi chandran wrote:I just had my doubts about new keyword being used for these static class.. Instance creation is meaningless in this case, isn't it?


Why do you think it is meaningless??? Can you give some reasons for the same???


As far as I know, new keyword will be creating an object with some attributes and other members defined in the class. Now if a class is static, then instance will not have its usual meaning, right?
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:if a class is static, then instance will not have its usual meaning, right?

Why??? its a inner class, a member of a class....

Gaurangkumar Khalasi wrote:By declaring inner class(i.e. B2) as static, we are trying to say that it is a Class variable and its object will be shared by each and every objects of Outer class(in your case it is Broom).

Please refer my above post, you will get the answer.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:
As far as I know, new keyword will be creating an object with some attributes and other members defined in the class. Now if a class is static, then instance will not have its usual meaning, right?


No, the static keyword does not affect how the inner class' functionality or how it can be created, it only affects the context in which the class is defined and be accessed from. You can still make instances of the inner class, and those instances will behave normally.

The difference is the context through which it must be called - in this case, through the BigOuter static context, rather than simply through the package's context.


And calling new B2() this way works the same way. Inside of Broom.main() you are already inside Broom's static context. So calling new B2() is OK and would be the same as calling

from some other context.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:
s ravi chandran wrote:
As far as I know, new keyword will be creating an object with some attributes and other members defined in the class. Now if a class is static, then instance will not have its usual meaning, right?


No, the static keyword does not affect how the inner class' functionality or how it can be created, it only affects the context in which the class is defined and be accessed from. You can still make instances of the inner class, and those instances will behave normally.

The difference is the context through which it must be called - in this case, through the BigOuter static context, rather than simply through the package's context.


And calling new B2() this way works the same way. Inside of Broom.main() you are already inside Broom's static context. So calling new B2() is OK and would be the same as calling

from some other context.


Thanks for replying. I guess, I am forgetting the that this is an Inner Class.. Will have to burn it in my mind.. so, I can conclude that static Inner Classes can be instantiated, the same way as other inner classes.. with the static limitation..
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!