Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding private

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Vishal
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
santhos, you are asking an impossible thing.
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4363
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic