2

Possible Duplicate:
Is it possible in Java to access private fields via reflection

Is there any way so that we can call the private data members of a class in java, can be accessed outside the class. I want this for a tricky question banks. As much my java experience i think this is possible, but i don't know how to do it.

Community
  • 1
  • 1
Aarun
  • 564
  • 1
  • 6
  • 13
  • 4
    see [this](http://stackoverflow.com/q/1555658/1787809) or [that](http://stackoverflow.com/q/1771744/1787809) – Nabil A. Dec 26 '12 at 07:11

7 Answers7

8

1) You can do it with reflection, if SecurityManager allows

class B {
    private int x = 2;
}

public class A {

    public static void main(String[] args) throws Exception {
        Field f = B.class.getDeclaredField("x");
        f.setAccessible(true);
        f.get(new B());
    }
}

2) in case of inner classes

class A {
    private int a = 1;

    class B {
        private int b = 2;

        private void xxx() {
            int i = new A().a;
        };
    }

    private void aaa() {
        int i = new B().b;
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
6

As per the java language specification, 3rd edition:

6.6.8 Example: private Fields, Methods, and Constructors

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. It is not inherited by subclasses.

You can use reflection in java to access private fields. Ideally, you should be using public setters and getter methods to access such data from outside the class (as others have posted)

Swapnil
  • 8,201
  • 4
  • 38
  • 57
  • What about security then? Main motive of Class concept is to restrict access the fields directly. If there is way to access private fields/methods then how security is ensured in Java? – doga Aug 17 '17 at 10:23
1

for more info about access modifiers check below link

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

and this for Accessing private variables in Java via reflection

Accessing private variables in Java via reflection

Community
  • 1
  • 1
Ashkan
  • 320
  • 2
  • 17
0
  1. No private member can't be used outside the class
  2. You can use getter and setter methods for this purpose
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • i know this dear that private is not used out side the class but i need a code which make it possible. 'class Other {private String str; public void setStr(String value) { str = value; } } class Test { public static void main(String[] args) // Just for the ease of a throwaway test. Don't // do this normally! throws Exception { Other t = new Other(); t.setStr("hi"); Field field = Other.class.getDeclaredField("str"); field.setAccessible(true); Object value = field.get(t); System.out.println(value); } }' – Aarun Dec 26 '12 at 10:11
  • 1
    i know this dear,but thats not i am questioning.so please read question properly – Aarun Dec 26 '12 at 10:25
  • @ArunKumarThakur yes dear.you are about to release java 8 ? sorry for answering your great question which is already -3 vote. – Android Killer Dec 26 '12 at 10:30
  • 1
    Sorry dear, i am again saying you don't understand what i am asking so please don't comment.and there is a way which we normally don't use but we can access private data members outside the class i.e by reflection. – Aarun Dec 26 '12 at 10:35
  • 1
    i hope that you understands what i am saying. – Aarun Dec 26 '12 at 10:45
0

You can do it using public methods that returns/set the private field value.

public Class A{

   private String myData;

   public String getMyData(){
        return myData;
   }

   public void setMyData(String data){
        this.myData=data;
   }
}
Community
  • 1
  • 1
Mawia
  • 4,220
  • 13
  • 40
  • 55
-1

Define a public get method to get private field of your class.

Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
-1

No you cannot, by any means access the private variables in java.

You can provide public getter and setter methods to access or change the value of the private member variables.

Yogesh Patil
  • 908
  • 4
  • 14