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 use return in constructor?

0 Answers  


what are the diffrences between interface and abstract class?

4 Answers  


How do you sort arraylist in descending order?

0 Answers  


Is there is any difference between a scrollbar and a scrollpane?

0 Answers  


Can Java be said to be the complete object-oriented programming language

1 Answers  


Can we call a non-static method from inside a static method?

0 Answers  


How does sublist works in java?

0 Answers  


what is real-time example of runtime polymorphism and compile time polymorphism

4 Answers   SLK Group,


Name the types of 'priority'?

2 Answers  


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

6 Answers   L&T, Wipro,


What is stack class in java?

0 Answers  


What do you understand by a Static Variable?

0 Answers   CGI,


Categories