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
How can you write a loop indefinitely in java programming?
What is reverse function?
How many JVMs can run on a single machine and what is the meaning of Just-In-Time (JIT) compiler?
When do you call copy constructor?
Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?
Difference between method overloading and method overriding in java ?
What is 16 bits called?
What is a boolean in java?
What is the difference between quicksort & mergesort? When should they be used? What is their running time?
What is the difference between a loader and a compiler?
Write the algorithm to check the number non-leaf nodes in a tree.
What is the simpletimezone class in java programming?
List implementations of list interface?
Explain the pointers in Java?
What is the difference between scrollbar and scrollpane?