Solved

Accessing private class variables

Posted on 2003-12-01
9
1,285 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

688 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