• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in static

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Vijitha Kumara for your reply.,
 
reply
    Bookmark Topic Watch Topic
  • New Topic