.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Programming (http://forums.bots-united.com/forumdisplay.php?f=25)
-   -   Calling methods from different classes (http://forums.bots-united.com/showthread.php?t=1951)

Zacker 12-06-2004 17:56

Calling methods from different classes
 
I have a method which can add a value to a variable. Calling it works fine, it adds the number to the variable and everything is good.
If the game calls the method from another class, then it looks like it adds it to a new variable.

Example:
Class1:
Add 50 to life. Life is now 50.
Add 50 more to life. Life is now 100.

Class2:
Add 20 to life. Life is now 20.
Add 50 to life. Life is now 70.

I dont understand how this can happen, as the variable only gets declared in the class with the aforemented method.

Edit:
The lang is Java.

MusicMan 12-06-2004 18:16

Re: Calling methods from different classes
 
Code:

#include <iostream.h>
 
class Class1 //declaring first class
{
 
public:
 
int addvalues (int a, int b) { return (a+b); } //creating method in first class
};
 
class Class2 //declaring second class
{
public:
Class1 object1; // creating an object of Class1 in Class2
 
};
 
int main()
{
 
Class2 object2; // creating an object of Class2
 
cout << object2.object1.addvalues(5, 6); //calling method in Class1 using the object of Class1 declared in Class2
 
return 0; //ending program
 
}

Maybe this little piece of code I wrote can help you understand?

edit: added comments

Zacker 12-06-2004 21:37

Re: Calling methods from different classes
 
The method declaration lies within Race.Java
Quote:

//Justerer eller returnerer spillerens liv
public int fixliv(int a)
{
if(a!=0)
{
//Giver spilleren a mere i liv
liv = liv + a;
System.out.println("Du har nu " + liv + " i liv");
}
else
{
//Returnerer værdien af liv
return liv;
}
return 0;
}
First it gets called in Race.java
Quote:

fixliv(100);

Then I declare a new instance of the class Race in Sted.java:
Quote:

//Race
//Opret et objekt af klassen Race
Race race = new Race();
And then I call it in Sted.java:
Quote:

race.fixliv(10);

botman 12-06-2004 22:07

Re: Calling methods from different classes
 
"new Race()" creates a new object.

You have 2 different instances of the same type of class. Each of those 2 instances have their own member variables. Changing a member variable in one class won't change the same member variable in another class.

botman

Zacker 12-06-2004 22:56

Re: Calling methods from different classes
 
Thanks for the replies. After a good talk with Musicman on MSN, we were able to work it out.

The variable liv needs to be declared as static. That prevents what Botman explained from happening.

It is done the same way in both Java and c++:
Code:

static int liv;

Austin 13-06-2004 10:30

Re: Calling methods from different classes
 
Think of it this way.

For classes...

When you declare a static member variable (or static member function) there is only ONE variable (or function) SHARED between all instances of that class currently instantiated at any given point in time.


All times are GMT +2. The time now is 01:26.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.