jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Regarding private Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding private" Watch "Regarding private" New topic
Author

Regarding private

santhosh kumar vk
Ranch Hand

Joined: Aug 25, 2009
Posts: 146
Hi,

Could you please tell me how to access private variable in another class without using getter and setter.

Tanks,
Santhosh Kumar V.K
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

This is not possible unless the "another class" is an inner class

Vishal


Programming is about thinking, NOT coding
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

santhosh kumar vk wrote:Hi,

Could you please tell me how to access private variable in another class without using getter and setter.

Tanks,
Santhosh Kumar V.K


Private variables are private to the class (hence, the name). You can't access it directly from outside of the declared class.

You can only access private variable indirectly (such as using getters) or via the reflection library (then the security manager allows it, of course).

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You can’t access private fields outside the class at all. A public getter method allow public access, but that is only indirect. It is impossible using ordinary programming.
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

santhos, you are asking an impossible thing.


PM Me If You Want to get Urgent Help on Java Programming
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
In Reflection i am able to get few details of private variable, could we also get data of it ? need to hit with hammer more to get this but i think JDK kicks back



Regards
Azrael Noor
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Azrael Noor wrote:In Reflection i am able to get few details of private variable, could we also get data of it ?

Like Henry said, it is possible to get the value from a private variable in a different class using reflection, but the Security Manager has to allow it - so you can't be sure it will always work unless you can also control the security manager the application runs in.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
When I, earlier wrote: . . . ordinary programming.
… I meant not using reflection. You can cause no end of trouble by using reflection to circumvent somebody else’s programming style.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

as Vishal said, inner classes have direct access to private members of the outer class. another option is, if both classes are in the same package, make the member variable package access(no modifier) and give the "want's access class" a constructor that takes the "wanted class as a parameter.


SCJP
Visit my download page
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Azrael Noor wrote:In Reflection i am able to get few details of private variable, could we also get data of it ? need to hit with hammer more to get this but i think JDK kicks back


Well, have you tried the many other methods of the java.lang.reflect.Field class? Besides getName() and getType() that is.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding private