How to access a variable if it is declared as private?
Answer Posted / ranganathkini
If a variable inside a class is marked private then it
cannot be accessed directly.
Access to this variable can only be provided by a public
accessor and mutator methods. Example:
class MyClass {
private int dataValue;
public int getDataValue {
return dataValue;
}
public void setDataValue( int newValue ) {
dataValue = newValue;
}
}
| Is This Answer Correct ? | 7 Yes | 2 No |
Post New Answer View All Answers
When should I use stringbuffer?
Explain the use of shift operator in java. Can you give some examples?
Can we split string with in java?
What are the differences between throw and throws?
What is the technique adopted to create an immutable class?
Can we declare a class as abstract without having any abstract method?
Explain the difference between serializable and externalizable in java?
What is a private class in java?
What is compareto?
What is the difference between notify and notifyall method?
Can we override constructors?
What does indexof mean?
How do you check if an arraylist is not empty?
What does yield method of the thread class do?
What are the advantages of functions?