6
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by richieking
0

Static methods can not directly access any instance variables or methods but they can access them by using their object reference.
Static methods may even access private instance variables via a object reference.
All private instance variables are private to its class, they can be accessed by static methods or non-static method of the class.

0

how is it that main can access instance members?

Do you have an example where that is done? Please post it.

0

Here's an example:

public class MyClass {
   // member variable can't be accessed directly in static method
   private String name;

   public static void main(String[] args) {
      // create an object to access its member variables
      MyClass test = new MyClass();
      test.name = "any name";
      // ... do some other stuff
   }
}
0

although most would perfer the use of mutators instead of directly trying to access (private) instance variables

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.