Solved

Accessing private class variables

Posted on 2003-12-01
9
1,277 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
 
LVL 4

Expert Comment

by:Howie_Ly
ID: 9855852
> how about using protected ????
only good if you're extending the class
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now