• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in static

 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can't access the instance variables inside the static method.,
but in main() method we can create new objects to the class and we can access the instances of the class.,

so my doubt is for main() method is it an exception??
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r suraaj wrote:but in main() method we can create new objects to the class and we can access the instances of the class.,

Have you tried this in static method other than main() method?
 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes., i tried.,

other than main() method other static methods doesn't have the access to instance variables and non-static methods.
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
r suraaj wrote:yes., i tried.,
other than main() method other static methods doesn't have the access to instance variables and non-static methods.

Main method is not behaving differently from other static methods, it's just the starting point where JVM looks for when running a class. Neither of them have access to the instance fields etc.., but as you said earlier by creating an instance it's possible to access that instance's fields either in normal static method or in main() method.
 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Vijitha Kumara for your reply.,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic