• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static in Java  RSS feed

 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



How main method (static) can access non-static(instance) vaiable? As it should not access.

Please help me in clearing above concept.
Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody asked virtually the same question last week. Look here, and see if that helps
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Campbell, Thanks for Pronpt reply.

I refered the link. From that link i conclude(understood) following:

"[/LIST]You can create nonstatic member inside static method and through those non-static(instance) members you can access non-static members in static method."


Is my understanding correct?
 
Leslie Viviani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works because you have instantiated the class A.

The following would not work for the reason that the method is static and the variable is not:
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you writeyou are not creating a static variable and you are not creating a non-static variable. You are creating a local variable. Then you are using that local variable to create other variables, which can be static, non-static, and as many as you can fit into your RAM
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!