passing variables between classes

I am writing a pretest that is 2 pages. afterthe first page the user picks his answers and hits the "save" button and then the 2nd page comes up, he picks his answers, and then he hits "submit" to finish.

how do i save the answers from class of the first page and get them to the class on the second page so I can check them when submit is pressed?

public class page1 extends javax.swing.JFrame

if(JRadiobutton1.isSelected())
        answer[1] = 1;

etc.

answer[1] = 3
answer[2]= 4

public class page2 extends javax.swing.JFrame

answer[3] = 4
answer[4] = 1

answer[1] and answer[2] need to be passed to class page2 somehow so i can check the answers



jpking72Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hoomanvCommented:
you dont need to pass answer array to second page
store all the answers in your current application frame
but instead change the frame that is shown each time to the person
use a CardLayout to change pretest's pages
hoomanvCommented:
I mean dont create a new frame for each page
have them all in one frame but switch them using CardLayout or just JTabbedPane
jpking72Author Commented:
isn't there a way to just pass variables from class to class?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

hoomanvCommented:
> isn't there a way to just pass variables from class to class?
use a method in class2 and call it from class1 to pass variables to class2

class B {
   void getVars(vars) {
      this.vars = vars;
   }
}

class A {
   void passVars(vars) {
      B b = new B();
      b.getVars(vars);
   }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
d3storCommented:
I suppose you're calling your page-classes from a different class, right?
If so, you can add

publi int[] getAnswers() {
   return this.answers();
}

to your page1 -class
and make a constructor or method for page2 -class which accepts those vars.

-

A bit better approach, however, is to add getAnswers() -method to both page-classes and
do the validating / processing at the calling class or create an observable value object and controller for that object.
Mayank SAssociate Director - Product EngineeringCommented:
>> isn't there a way to just pass variables from class to class?

Well, you can maintain the answers at a global level in some static collection.

public class AnswersList // make it a singleton
{
  private static AnswersList instance ;
  private Hashtable answers ;
  private AnswersList ()
  {
    answers = new Hashtable () ;
  }
  public Hashtable getAnswers ()
  {
    return answers ;
  }
  // give a static synchronized getInstance ()
}

You can use this hash-table in all the frames for storing answers (values) along with question-numbers (keys) at a global level.
jpking72Author Commented:
all you have to do is put it in the constructor:

UserProfile up = new UserProfile(level,start);

================================

public class UserProfile {
   
    /** Creates a new instance of UserProfile */
    private String level;
    private int start;
    public UserProfile(String a,int b) {
            level = a;
            start = b;
    }
    public String getLevel(){
        return level;
    }
    public int getFuntion(){
        return start;
    }
}
Mayank SAssociate Director - Product EngineeringCommented:
? What is UserProfile?
jpking72Author Commented:
UserProfile is supposed to send sends the "level" and "start" to the "Startpage" class. my question now is how do you call method "getLevel" from outside the class?  how do you "pass" the level and start to a new class (say class "Test")? I'm looking for line /**********/.


public class test {
   
    /** Creates a new instance of test */
    public test() {
    }
   
    String level = up.getLevel();  /**************/
}



================================
public class Startpage
...
String level = "beginner";
int start = 5;
...
UserProfile up = new UserProfile(level,start);

================================

public class UserProfile {
   
    /** Creates a new instance of UserProfile */
    private String level;
    private int start;
    public UserProfile(String a,int b) {
            level = a;
            start = b;
    }
    public String getLevel(){
        return level;
    }
    public int getFuntion(){
        return start;
    }
}
Mayank SAssociate Director - Product EngineeringCommented:
>> String level = up.getLevel();  

Is correct. You need to write it inside a method where you will define an object of UserProfile called 'up'.
jpking72Author Commented:
it's still not clear.
Mayank SAssociate Director - Product EngineeringCommented:
You are writing:

>> String level = up.getLevel();

in the wrong place. It should be inside a method, like:

public test ()
{
  UserProfile up = new UserProfile () ;
  String level = up.getLevel () ;
  System.out.println ( level ) ;

}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.