Which of the following is accessible by instance methods ?(a) Only instance variables
(b) Only class variables(c) Both instance variables and class variables
(d) All of the above
Answers
Q:- Which of the following is accessible by instance methods ?(a) Only instance variables
(b) Only class variables(c) Both instance variables and class variables
(d) All of the above
Ans:- All of the above
‘Instance method’ can access both instance variables and class variables.
Answer: Option (c)
Explanation:
There can be two ways. They are as follows,
1) Instance methods can access ‘instance methods’ and ‘instance variables’ directly.
The instance methods are those which require class objects to be created before its call. In order to invoke the instance method, an ‘object of the class’ has to be created within the definition. These ‘instance variables’ are declared in a class, but ‘outside the constructor’ or a method.
2) Instance methods access class methods and class variables directly.
The ‘class variables’ are also known as ‘static variables’. These ‘static variables’ are declared using the ‘static keyword’ in a class but outside a method.