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

What is the use of beaninfo?

793


What is the reason behind using constructors and destructors?

800


Can java list be null?

751


How many characters is 16 bytes?

758


How many return statement are allowed in a function?

654


what is the purpose of using rmisecuritymanager in rmi?

766


Is java free for commercial?

669


Can we serialize singleton class?

772


Why is logger singleton?

738


What is the primitive type byte?

774


What is difference between arraylist and list in java?

812


Is it possible to write a regular expression to check if string is a number?

780


Where to store local variables?

786


What is static method with example?

868


Give an example of use of pointers in java class.

763