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

what is thread? What are the high-level thread states? : Java thread

0 Answers  


What are the types of java?

0 Answers  


How do I find and replace in word?

0 Answers  


What is an abstract class?

8 Answers   DBS, Wipro,


What is collections framework?

0 Answers  


What is meant by binding in rmi?

0 Answers  


In a container there are 5 components. I want to display all the component names, how will you do that?

0 Answers  


What are the two ways to create a thread?

0 Answers  


What is a line break?

0 Answers  


Can an anonymous class be declared as implementing an interface and extending a class?

2 Answers  


Can you add null to a list java?

0 Answers  


What do you mean by ordered and sorted in collections in java?

0 Answers  


Categories