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
Why java is call by value?
Why string is immutable or final in java
What is nan in java?
What is string data type?
What is string data?
Give few difference between constructor and method?
Why char array is preferred over string for storing password?
Is arraylist dynamic in java?
How many bits is a string?
Can a main method be declared final?
What are inbuilt functions in java?
What is keyword in oop?
Explain the access modifiers for a class, method and variables?
List the features of java programming language.
What is jpa specification?