• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Regarding private

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Vishal
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
santhos, you are asking an impossible thing.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74376
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic