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


Please Help Members By Posting Answers For Below Questions

Can we override tostring method in java?

732


Can we execute a program without main() method?

778


What is a qms manual?

774


What do you understand by the term string pool?

789


Is integer immutable in java?

763


why java does not support unsigned keyword?

4733


Explain inheritance in java?

798


How many types of memory areas are allocated by JVM in java?

817


Why java is used everywhere?

776


What is array and arraylist in java?

809


What methodology can be employed to locate substrings inside a string?

746


What is instance example?

738


Give reasons supporting that string is immutable.

732


What is bool mean?

766


What is difference between null and void?

744