chatteVerte
chatteVerte

Reputation: 13

Java - how to use the same object in many classes

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 !

Upvotes: 1

Views: 5952

Answers (6)

BK Batchelor
BK Batchelor

Reputation: 457

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;
        }
}

Upvotes: 0

Paras Dulal
Paras Dulal

Reputation: 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
  }
}

Upvotes: -1

David
David

Reputation: 153

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.

Upvotes: 2

HadesDX
HadesDX

Reputation: 168

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

Upvotes: 0

Razib
Razib

Reputation: 11163

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.

Upvotes: 0

adrCoder
adrCoder

Reputation: 3275

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

Upvotes: 0

Related Questions