File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Static variables and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static variables and methods" Watch "Static variables and methods" New topic

Static variables and methods

sudharani Savadi
Ranch Hand

Joined: Feb 23, 2011
Posts: 30

in java a static method can access only static data members but main() is a static method
still it is accessing the instance data members by creating an object of other class.
how can it be possible?"
Matthew Brown

Joined: Apr 06, 2010
Posts: 4363

It can only access static data members without an object reference. But if you've got an object reference, non-static members and methods can be accessed via that.


The distinction is that in non-static methods there is an implicit object reference (this), meaning you can access non-static members of the same object without a reference.

I'll move this to the Beginning Java forum, as it's more appropriate there.
sudharani Savadi
Ranch Hand

Joined: Feb 23, 2011
Posts: 30

Thank you.
I agree. Here's the link:
subject: Static variables and methods