Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Accessing private class variables

Posted on 2003-12-01
9
Medium Priority
?
1,294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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