?
Solved

Accessing private class variables

Posted on 2003-12-01
9
Medium Priority
?
1,288 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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