GeeCON Prague 2014*
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
Author

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
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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.

E.g.

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.
 
GeeCON Prague 2014
 
subject: Static variables and methods