Solved

Accessing private class variables

Posted on 2003-12-01
9
1,281 Views
Last Modified: 2013-11-23
Hello.

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.
Rick.
0
Comment
Question by:Cheney
9 Comments
 
LVL 2

Author Comment

by:Cheney
ID: 9854619
Also, I cant make it public either.  I know this defeats the point of having private members.
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 9854623
you can access them using reflection.
0
 
LVL 5

Expert Comment

by:b_vishwajit
ID: 9855831
how about using protected ????
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9855852
> how about using protected ????
only good if you're extending the class
0
 
LVL 92

Expert Comment

by:objects
ID: 9855876
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();
            fields[i].setAccessible(true);
            System.out.println(field + " -value- " + fields[i].get(o));
        }
    }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 9857090
>> how about using protected ????
>only good if you're extending the class

  ... or under the same package.
0
 
LVL 92

Expert Comment

by:objects
ID: 9864209
0
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9864243
Nice code there objects.. i'm keeping it for future reference =)

~ Objects the Visible Difference, nice slogan  hehe
0
 
LVL 92

Expert Comment

by:objects
ID: 9864256
> Objects the Visible Difference, nice slogan  hehe

A marketing mate came up with that for me, I'll pass on your approval to him :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question