• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to access private static variables outside the class?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to access a variable outside a class which has been declared as private static (inside a class)?

For e.g.



This can be accessed using A.c

Similarly is there a way to acess a private static variable using class instance?

Please suggest if there is some approach apart from using the public static methods which can access this private static variable.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pooja Sinha wrote:Is there any way to access a variable outside a class which has been declared as private static (inside a class)?



If there was, what would be the meaning of declaring the variable private?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can access is with reflection. This is not part of the SCJP objectives

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Deepak: Isn't that called hacking?
 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:You can access is with reflection. This is not part of the SCJP objectives


Hi Deepak,

how, when and where this kind of access is applied in the real world? Does it gives us any advantages ?Thanks
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

@Deepak: Isn't that called hacking?



Nah. Its useful information. Like when some one wants to access that private static Singleton instance of yours, you now know they can do it.

how, when and where this kind of access is applied in the real world? Does it gives us any advantages ?Thanks



Reflection in general is applied across many useful libraries like commons beans utils. Accessing private member variables via reflection is not something that you will come across regularly.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Valentin Ivanov wrote:how, when and where this kind of access is applied in the real world? Does it gives us any advantages ?Thanks


You should not do this in any 'normal' program (I guess that's why Monu called it "hacking"). If a member of a class is private, there's probably a good reason why this is so, and you should not try to hack your way to it like this - that will make your program obscure and unmaintainable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic