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
What are the advantages of arraylist over arrays?
What is broken and continue statement?
What are green threads in java?
What function extracts specified characters from a string?
What is bitwise complement?
What is the difference between normal report & matrix report?
What is fail first in java?
What are the various access specifiers for java classes?
Is array synchronized in java?
What is a void method?
What is JVM and is it platform independent?
Explain the difference between treeset and treemap in java?
How we can generate random numbers in java?
How do I know if java is installed?
Why hashcode is used in java?