Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Question in my book is asking: What restrictions are placed on instance variable and static variable access from within the definition of: 1.) An instance method? 2.) A static method?

Is my response to this concept correct?

-An instance method cannot directly access the instance variable while a static variable can be directly accessed since one copy is used throughout the class. (Each object will share this static variable as well as the static methods in the class. An instance variable is only available to each object and each object has its own copy of this instance variable.) A static method cannot access instance members of the class. A static method can however access members of the static variable.

share|improve this question
1  
A static method can however access members of the static variable. This is a bit unfortunately worded. More clear would be "A static method can access static members", otherwise your interpretation seems to be correct. –  Jeroen Vannevel Mar 10 at 0:09
    
Now, members in this case is the same as variables right? –  zeesh91 Mar 10 at 0:12
1  
Yes your interpretation is correct. Also I agree to the statement of Jeroen. –  steven0529 Mar 10 at 0:12
    
members = instance variables or static variables? –  zeesh91 Mar 10 at 0:12
    
Members are everything that belongs to the class body. You can split this up further in instance fields, class fields, instance methods, class methods, constructors, etc. Where "class" is the same as "static". –  Jeroen Vannevel Mar 10 at 0:27

2 Answers 2

up vote 3 down vote accepted

An instance method cannot directly access the instance variable

Wrong.

while a static variable can be directly accessed since one copy is used throughout the class.

Correct.

(Each object will share this static variable as well as the static methods in the class.

Correct.

An instance variable is only available to each object and each object has its own copy of this instance variable.)

Correct.

A static method cannot access instance members of the class.

Correct.

A static method can however access members of the static variable.

Correct, if it has members, and they are accessible.

The compiler would have told you all this with 100% reliablity.

share|improve this answer

That's right, simply put:

Instance methods can access instance and static variables of the same class (if other access modifiers permit so);

Static methods can only access static variables of the same class.

share|improve this answer
    
well, in terms of only within the instance methods and static methods, are the restrictions described in my response spot on? ;) –  zeesh91 Mar 10 at 0:16
    
Yep. Last two sentences are just that :) –  Artem Mar 10 at 0:20
    
@Artem If what you say is right, which it is, what the OP says isn't right, which it isn't. They aren't equivalent. –  EJP Mar 10 at 0:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.