![]() |
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. |
Re: Calling methods from different classes
Code:
#include <iostream.h> edit: added comments |
Re: Calling methods from different classes
The method declaration lies within Race.Java
Quote:
Quote:
Then I declare a new instance of the class Race in Sted.java: Quote:
Quote:
|
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 |
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; |
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.