Accessing private class variables


Im having trouble finding a solution for the following problem:

I have a class A that contains a private variable:

class A
     private double variable;

I need to access variable (both get and set) from another class, WITHOUT adding get and set methods to the class.  I tried to use a second, inner, class as it too whould have access to the variable, however this was daft as you cannot instante inner classes of one class from another...

I need a way to access variable, whether it be through a second class or some kind of hack.
Thanks in advance for your assistance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CheneyAuthor Commented:
Also, I cant make it public either.  I know this defeats the point of having private members.
Mick BarryJava DeveloperCommented:
you can access them using reflection.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
how about using protected ????
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

> how about using protected ????
only good if you're extending the class
Mick BarryJava DeveloperCommented:
import java.lang.reflect.*;
Here's an example that reads all member vars of a class including private.

public class  ReadPrivateFields
    public static void main(String[] args)  throws Exception
        Class c = Class.forName(args[0]);
        Object o = c.newInstance();
        Field[] fields = c.getDeclaredFields();
        for (int i = 0; i < fields.length; i++)
            String key = fields[i].getName();
            System.out.println(field + " -value- " + fields[i].get(o));
>> how about using protected ????
>only good if you're extending the class

  ... or under the same package.
Mick BarryJava DeveloperCommented:
Nice code there objects.. i'm keeping it for future reference =)

~ Objects the Visible Difference, nice slogan  hehe
Mick BarryJava DeveloperCommented:
> Objects the Visible Difference, nice slogan  hehe

A marketing mate came up with that for me, I'll pass on your approval to him :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.