How to bind two classes in java?

How can I bind together two Java classes?

I'm programming a 3D opengl program where I have several different classes. I know how to bind classes to the main class, but I can't bind two separate classes together. I have a class Terrain where I have a list containing some data about the terrain. Then I need that data in another class called Figure which isn't the main class. I have tried to bind these classes together like this:

In Terrain class I have:

Figure fig;

public void bindClasses(Figure fg) {
   fig = fg;

And then in the Figure class I have:

Terrain ter;

public void bindTerrain(Terrain tr) {
   ter = tr;

And then in both classes I call those functions. Shouldn't that bind them and their variables? At least that's how I have bound my classes with the main class.


Just to start off with terminology. A class is a blueprint that tells you how an instantiated object is - the moment you write new Figure(), you've created an instance, an object of the class Figure (often you have several objects of one class). So when you are "binding" above, you are not actually binding classes, you are binding objects.

The above code is fine. By convention, you often write that sort of things with setters, it's not necessary to call them that, but a very common pattern is:

public class Terrain {

    Figure fig

    public void setFigure(Figure fig) {
       this.fig = fig;


public class Figure {

    Terrain ter

    public void setTerrain(Terrain ter) {
       this.ter = ter;


To associate the two together you would in your main class do, which, as you see, is pretty much exactly what you would have already, just using the conventional names.

public void init() {

   Terrain ter = new Terrain();   // create object of class Terrain
   Figure fig = new Figure();     // create object of class Figure



If there's a relationship between the two objects, for instance, you can't create a figure without having a terrain to put it in. Which would make terrain almost "own" figure. You could indicate this by using the constructor on figure.

public class Figure {

    Terrain ter

    // constructor requires an instance of Terrain, 
    // since the figure must always be placed in a terrain
    public Figure(Terrain ter) {

       this.ter = ter;

       // let terrain know this is the main figure.


Now your init code would instead look:

public void init() {

   Terrain ter = new Terrain();   // create object of class Terrain
   Figure fig = new Figure(ter);  // create object of class Figure in the terrain

   // no setters needed, since figure constructor sets up
   // the relationship.


two way binding like yours is possible but is "tricky" from a design points view as well as technical.

a way to achieve it is:

public void addFigureToTerrain(Figure f){

now I would recommend you think carefully if you need this kind of linkage between the classes ?

usually it is simpler and more practical to have a general object (terrain) that contains a list of objects(figures). then the general object interogates/updates the list (for examples: figure.hasRecievedEventAttack(attack) or figure.whatDoYouWantToDo()) and then the general object(terrain) analyses a answer

Need Your Help

onRmsChanged(float rmsdB)‬ to 0-100 scale


When I use onRmsChanged(float rmsdB)‬ of RecognitionListener I get it in root min squere dB. I want to convert it to the scale of 0 to 100. Can someone help?

Highcharts multiple x-Axis without multiple series?

javascript highcharts axes multiple-axes

This fiddle uses multiple series and displays an axis for each of them. Is it possible to use a single series to make several Axes?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.