1

I'm new here so please forgive possible mistakes :)

I'm writing a game as a final project for my coding classes. And...I'm really stuck. I want to create one object of certain class BUT later on I need to pass there different data from different other classes so I can save all data at the end of using a program.

For example I create an object in MainFrame and get a name of a user from there. Then I go to NextFrame and get age of a user etc etc.

I'd appreciate the answers in as simple english as possible, I'm not fluent :) I'm using netbeans btw. Thanks a lot !

chatteVerte
  • 13
  • 1
  • 5
  • Pass the objects to the methods you need them in and access the necessary fields. If you need the values of the properties of an object to create another object, create a constructor that requires that object and access its fields in the constructor. – Code Whisperer Mar 26 '15 at 20:32
  • You could pass a reference of you class to each other class or you could create a static utility class or create a singleton – MadProgrammer Mar 26 '15 at 20:37
  • Use the search key. http://stackoverflow.com/a/7923530/3498044 http://stackoverflow.com/a/7454611/3498044 http://stackoverflow.com/a/4253078/3498044 – priyank Mar 26 '15 at 20:53

6 Answers6

2

Simply try the Singleton Design Pattern.

Simple Example for that:

class SingletonClass {
    private static SingletonClass instance = null;
    private String customAttribute;

    public SingletonClass() {
        //default constructor stuff here
    }

    //important singleton function
    public static SingletonClass getInstance() {
        if(instance == null)
            instance = new SingletonClass();
        return instance;
    }

    // getter and setter
}

now, in your frame or any other class you just do the following:

SingletonClass myObject = SingletonClass.getInstance();

when this function is called for the first time, a new Object is created. Later, it returns the first created. With the help of the Singleton Pattern you can easily save data in one object across multiple classes.

for more information about Singleton: http://en.wikipedia.org/wiki/Singleton_pattern

hope this helps.

David
  • 153
  • 7
0

just pass the object to the class you want to, and use it accordingly in a method that you want to ! Here is an example with two classes:

class oneClass {
   void oneMethod() {
      Class1 myClass1 = new Class1();
      Class2 myClass2 = Class2 Class2();
      myClass2.setMyClass1(myClass1);
   }
}

class Class2 {
   Class1 myClass1;
   //...
   void setMyClass1(Class1 myClass1) {
     this.myClass1 = myClass1;
   }
   //...
   void doSomething() {
      // do something with instance variable myClass1
   } 
}

In your case Class1 can be MainFrame and Class2 can be NextFrame or however you want to call them...

As you can see from my code, you pass the class myClass1 to myClass2 using the following line of code : myClass2.setMyClass1(myClass1); and then you can work in this object any way you want

adrCoder
  • 3,145
  • 4
  • 31
  • 56
0

Just send the object of your MainFrame class using a method to wherever you want. The object will contains all data from whenever you change it from different method.

If you need a single object MainFrame all over the class then you may consider of using singleton pattern for creating the object.

Razib
  • 10,965
  • 11
  • 53
  • 80
0

to save things to a file(or stream) you can use interface serializable:

import java.io.Serializable;
import java.util.ArrayList;

public class Test implements Serializable {

public ArrayList<Object> urDiferentKindOfThings = new ArrayList<Object>();

public boolean add(Object o) {
    if (o != null) {
        urDiferentKindOfThings.add(o);
        return true;
    }
    return false;
}
}

Now, just add anything (Object!) that you want to save, then at the end of your game just save the object of type TEST that should contain all your stuff (you may need to read about serializable as it make life easy)

Good Look

HadesDX
  • 158
  • 6
0

You pass class instances into a managing class

public class Game {

    private MainFrame mainframe = null;
    private NextFrame nextframe = null;


    public Game(){
        this.mainFrame = new MainFrame();
        this.nextFrame =  new NextFrame();  
    }

    public Game(MainFrame mainFrame, NextFrame nextFrame){
        this.mainframe = mainFrame;
        this.nextframe = nextFrame;
    }    

    public String getName(){
        return mainFrame.getName();
    }

    public int getAge(){
        return nextFrame.getAge();
    }

}


public class MainFrame {
    private String name = "John"

    public String getName(){
        return name;
    }
}

public class NextFrame{
        private int age = 25;

        public int getAge(){
            return age;
        }
}
BK Batchelor
  • 457
  • 2
  • 6
-1
class a{
  function dosomething(){
     //code goes here
   }
}

class b{
    a firstobject=new a();
    c secondobject=new c(a objtopass);  //passing object of a to c
    function donext(){
        //next code
    }
}

class c{
a receivedobj=null;
public c(a objtoreceive){
    //constructor
    receivedobj=objtoreceive;
}
 function doAdd(){
   //function code
  }
}