How to access a variable if it is declared as private?

Answers were Sorted based on User's Feedback



How to access a variable if it is declared as private?..

Answer / 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

How to access a variable if it is declared as private?..

Answer / ravikiran(aptech mumbai)

the variable declared as private can be accessed with in the
same class only

Is This Answer Correct ?    2 Yes 2 No

How to access a variable if it is declared as private?..

Answer / neo

You can access the private variable in to the other package
or other classes by inheritance . to inherite the super
classes all the resources comes in to sub classes private
also in this context.

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Core Java Interview Questions

Can we assign null to double in java?

0 Answers  


What is anagram number?

0 Answers  


What is public/private protected in java?

0 Answers  


Java is based on pass by reference or pass by value ..

6 Answers   L&T, Wipro,


What is difference between final and finally in java?

0 Answers   Cyient,






Variable of the boolean type is automatically initialized as?

0 Answers  


What is the purpose of finalization in java programming?

0 Answers  


How can u increase the heap size in the memory?

0 Answers   Infosys,


they asked class A{} class B{} class c{} all the three class saved as a single file,there is no main method in the file and anothe class M.java class m { psvm(String args[]) { // here the parent class can access } }

2 Answers   DNS, IPSR Solutions,


What is illegal identifier in java?

0 Answers  


If we don’t want some of the fields not to serialize how to do that?

0 Answers  


How to calculate the length of a singly linked list in java?

0 Answers  


Categories