I have a small problem with doing calculations in one java class and trying to refer to the varibles in another class. I have written a GUI program that performs certain calculations and then when a button is pressed, the values obtained are saved as variables in another Java class ( called Calculations.class) . What I need to know is how can I access these variables again from another class so they can be used in further calculations.

Recommended Answers

All 2 Replies

It depends on how your classes are designed. Can you use inheritance or packages to allow access? Otherwise you're either going to be hosed, or you have to make the members public.

you can simply make an object of the class you want to use ... like this

public class1{

      WantedVariableClass wvc = new WantedVariableClass();
   
      //now you can access the variables like this
      wvc.variable1 = 10 + 90;
      wvc.variable2 = 10;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.