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 does pointer mean?

539


What is java oops?

549


What is variable and example?

513


What is google full form?

532


What is used of static keyword in java?

581






Does java support multiple inheritance or not?

593


List the features of java programming language.

556


How many threads can java run?

554


What are loops in java? What are three types of loops?

545


What is thread count in java?

541


What is stream api in java8?

531


Can you explain inner class.

586


What technique can be employed to compare two strings?

567


Can we sort hashset in java?

620


Explain method local inner classes ?

587