Page 1 of 3 123 LastLast
Results 1 to 20 of 55
Like Tree1Likes

Thread: Can't call variable from other class

  1. #1
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Can't call variable from other class

    Hello.
    I am doing a small game in java.
    It is quite similar to Pokemon, where the user cast a spell in order to inflict damage to the enemy.

    I am trying to do with a lot of classes to facilitate the access and a future development of the program and code. And in one class I need to use the variable of another.

    I write the code correctly but the program can't "call" the variable and it doesn't read it. I used the same code a few times in the same program and it doesn't work only in that class, in all the times I use.

    Here is the code of the relevant classes for this case: If you need more just say plz. It is for a school project so help us if u can :/.

    Java Code:
    public class Ataques {
    
    	
    	int Att;
    	double damage;
    	double defense;
    	double attack;
    	double hp;
    	double critical;
    	double base;
    	int champRef;
    	int champno;
    
    	public void ataque() {
    
    		Permis PermisObject = new Permis();
    		Combate CombateObject = new Combate();
    		
    		champno = CombateObject.champno;  //**FOR JAVA FORUM: The progam doesn't read this variable champno.
    		
    
    		// *Designar Champ 1 e champ 2
    		
    		System.out.println("LOL" + champno);
    		if (champno == 1) {
    			attack = CombateObject.CurrAta1;    //**It can't also read these. Even if i do a champno = 1 before that "if". 
    			defense = CombateObject.CurrDef2;
    			hp = CombateObject.CurrHP2;
    			critical = CombateObject.CurrCrit1;
    			champRef = CombateObject.champ1Ref;
    		}
    
    		if (champno == 2)
    
    		{
    			attack = CombateObject.CurrAta2;
    			defense = CombateObject.CurrDef1;
    			hp = CombateObject.CurrHP1;
    			critical = CombateObject.CurrCrit2;
    			champRef = CombateObject.champ2Ref;
    		}
    
    		// *ESPAÇO PARA LISTA DE ATAQUES PERMITIDOS!!!!
    
    		//
    
    		// *ESCOLHA DO ATAQUE!
    		System.out.println("Escolha o Ataque!" + champRef);
    
    		switch (champRef) {
    
    		case 1:
    			// Antonio
    			PermisObject.antonio();
    
    			break;
    		default:
    			System.out.println("ERRO");
    			break;
    		}
    
    	}
    
    }
    As it can't read any variable from "combate" class so i supose the problem must be in it:

    Java Code:
    public class Combate {
    	double CurrHP1, CurrHP2, CurrAta1, CurrAta2, CurrDef1, CurrDef2, CurrCrit1,
    			CurrCrit2;
    	String Champ1, Champ2;
    	int fim = 0, champ1Ref, champ2Ref;
    	int champno;  
    
    	// *Escolher abilidade/Item
    	public void combate() {
    
    		Champs ChampsObject = new Champs();
    		Ataques AtaquesObject = new Ataques();
    
    		// *Pre Combate:
    		// **Status
    		// *Champ1
    		System.out.println("JOGADOR 1");
    
    		ChampsObject.EscolherPersonagem();
    
    		Champ1 = ChampsObject.Champ;
    		champ1Ref = ChampsObject.champRef;
    		CurrHP1 = ChampsObject.HP;
    		CurrDef1 = ChampsObject.Defense;
    		CurrAta1 = ChampsObject.Attack;
    		CurrCrit1 = ChampsObject.Critical;
    
    		// *Champ2
    		System.out.println("JOGADOR 2");
    
    		ChampsObject.EscolherPersonagem();
    
    		Champ2 = ChampsObject.Champ;
    		champ2Ref = ChampsObject.champRef;
    		CurrHP2 = ChampsObject.HP;
    		CurrDef2 = ChampsObject.Defense;
    		CurrAta2 = ChampsObject.Attack;
    		CurrCrit2 = ChampsObject.Critical;
    		System.out.println(Champ1 + CurrHP1 + " VS. " + Champ2 + CurrHP2);
    
    		loop: while (CurrHP1 > 0 && CurrHP2 > 0) {
    
    			// *Champ1
    			champno = 1;
    
    			System.out.println("chasdasjdklaw" + champ1Ref + " " + champ2Ref + champno);
    
    			AtaquesObject.ataque();
    
    			// *Escolher acção
    			CurrHP2 = (CurrHP2 - (AtaquesObject.damage));
    
    			// *Verificações de Vitória
    			if (CurrHP1 <= 0 && CurrHP2 > 0) {
    				System.out.println("Vitória do " + Champ2);
    				fim = 1;
    				CurrHP1 = 0.0;
    				break loop;
    			}
    			if (CurrHP1 > 0 && CurrHP2 <= 0) {
    				System.out.println("Vitória do " + Champ1);
    				CurrHP2 = 0.0;
    				fim = 1;
    				break loop;
    			}
    			if (CurrHP1 <= 0 && CurrHP2 <= 0) {
    				System.out.println("Empate");
    				CurrHP1 = 0.0;
    				CurrHP2 = 0.0;
    				fim = 1;
    				break loop;
    			}
    
    			// *Informar HP
    
    			System.out.println("HP do " + Champ1 + " = " + CurrHP1);
    			System.out.println("HP do " + Champ2 + " = " + CurrHP2);
    
    			// *Champ2
    			champno = 2;
    
    			AtaquesObject.ataque();
    			CurrHP1 = (CurrHP1 - (AtaquesObject.damage));
    
    			// *Verificações de Vitória
    			if (CurrHP1 <= 0 && CurrHP2 > 0) {
    				System.out.println("Vitória do " + Champ2);
    				fim = 1;
    				break loop;
    
    			}
    			if (CurrHP1 > 0 && CurrHP2 <= 0) {
    				System.out.println("Vitória do " + Champ1);
    				fim = 1;
    				break loop;
    			}
    			if (CurrHP1 <= 0 && CurrHP2 <= 0) {
    				System.out.println("Empate");
    				fim = 1;
    				break loop;
    
    			}
    
    			// *Informar HP
    
    			System.out.println("HP do " + Champ1 + " = " + CurrHP1);
    			System.out.println("HP do " + Champ2 + " = " + CurrHP2);
    		}
    
    		// *Iterromper batalha
    
    	}
    }
    There is more classes if you need them i will post it. I only post these 2 since i think they are the more relevant.
    I hope you can help me, you are my last resort.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    the program can't "call" the variable
    What variable are you trying to "call"? What class is it in and from what method are you trying to "call" it?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Combate CombateObject = new Combate();
    champno = CombateObject.champno;

    I Create the CombateObject so i can use objects from the class "Combate"
    The variable i am trying to use is champno from Combate, but all variable i try to bring from "Combate" to the class "Ataques" just don't work. Simply acts as it has no value.

  4. #4
    zonic is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Can't call variable from other class

    Combate class has a method called combate() which actually manipulate the value of champnovariable depend upon the checks that you have introduced.

    if i look on your class Ataques which creating the object for class Combate . Now you are accessing the variable champno before calling the method combate()
    ( CombateObject.combate()) which results in giving the value as 0 only .

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    When you create a new instance of a class, all its variables will have their initial values. If you want to get to the contents of an existing object, you need to have a reference to that object. You need a copy of the variable that has the address of an existing Combate object. Can you pass it in a call to a constructor or method?
    zonic likes this.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    But the class Ataques run inside the Class Combate. Do i need to run it again somehow?


    champno = 1; //* value 1 is gave to champno

    AtaquesObject.ataque(); //*the other class method is starting now.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    Please explain what "run inside" means? If the Ataques class is an inner class to the Combate class, then the methods in the Ataques class can access the variables in the Combate class.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,354
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can't call variable from other class

    If a member variable is a public static variable you only need the class to read or write the variable:

    Java Code:
    CombateObject.champno= 42;
    But if that variable isn't static you need an object (instantiation of the class) to read or write the variable:

    Java Code:
    CombateObject theObject= new CombateObject( ... );
    theObject.champno= 42;
    The variable belongs to an object and if you don't have an object (of that class) you're simply out of luck. Note that accessing a variable like this is a dangerous thing, because you're reading or altering the value of that variable without the object knowing about it and maybe the object doesn't want it to be altered or read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    zonic is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Can't call variable from other class

    Java Code:
    public class pojo {
    
    	int i ;
    	
    	public int getI() {
    		return i;
    	}
    	public void setI(int i) {
    		this.i = i;
    	}
    	
    }
    Above class is having variable which will be used by class test

    Java Code:
    public class test {
    
    	public static void main(String[] args) {
     pojo p = new pojo();
     p.setI(20);                //use setter method to set the value
     System.out.println(p.getI());  //use getter method to get the value    
    
    //here value for the i will be print as 20
    	}
    
    }

    Above example might help you to solve yours problem

  10. #10
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Please explain what "run inside" means? If the Ataques class is an inner class to the Combate class, then the methods in the Ataques class can access the variables in the Combate class.
    What i meant is that the method ataque from the class Ataques runs inside the combate class. While the combate class runs it will activate the method

    I created an object as JosAH said
    Java Code:
                // *Champ1
                champno = 1;  //**It the variable dat i want to read in the Ataques class
     
                System.out.println("chasdasjdklaw" + champ1Ref + " " + champ2Ref + champno); //** This is only a test println checking if everything is rigth.
     
                AtaquesObject.ataque(); //** This is the object. Here starts running ataque method from ataques class.
    Java Code:
        public void ataque() { //**the method ataque
     
            Permis PermisObject = new Permis(); //** this is for starting another method from other class in the future.
            Combate CombateObject = new Combate(); //** this is the object for calling champno from combate class.
             
            champno = CombateObject.champno;  //**This is champno from this class, taking the value from champno from combate class. it just don't recognize the champno from Combate.

    Zonic, i am a rly starter in Java.
    The main method where the program starts is test. rigth?
    It's created an object to access pojo class. and p.setI means that setI method will run and give the variable i the value 20. and what does the command return? It gives the value of the variable? Can i use the command return to pick up the value of champno from Combate class?
    Last edited by Rsmpt; 04-16-2012 at 10:45 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    Are you trying to use/access the Combate class's champno variable from a method in the Ataques class?
    Does that class have a reference to an instance of the Combate class?
    If it does, then use that reference to access the variable in the Combate class.
    If it does not have a reference to an instance of the Combate class, where is there a reference that could be passed to the Ataques class? When you create an instance of the Ataques class from the Combate class, you could pass the constructor a reference to the current Combate class object by passing the "this" reference to the Ataque class's constructor and have the constructor save that reference in a local variable in the Ataque class.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    But can't the:

    Java Code:
    Combate CombateObject = new Combate();
    be considered a reference to an instance of the class?

    Im completely lost here:

    When you create an instance of the Ataques class from the Combate class, you could pass the constructor a reference to the current Combate class object by passing the "this" reference to the Ataque class's constructor and have the constructor save that reference in a local variable in the Ataque class.
    Because, from the Combate Class I'm calling the ataque() method from the Ataques class to where I want to bring the "champno" variable, located in the Combate Class. For that Im using the CombateObject created in the Ataque class...

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    Yes the following will create an instance of the Combate class:
    Combate CombateObject = new Combate();

    Will that instance have the data that you want to access? Or is the data in another instance of the class?
    You need to get a reference to the instance that has the data that you want access to.

    Where is there a reference to an instance of the Combate class that has the data you want access to?
    Remember that "this" refers to the current instance of a class.

    calling the ataque() method from the Ataques class to where I want to bring the "champno" variable
    If you are calling the ataque() method from the Combate class, you could pass the value of champno as a parameter in that call: ataque(champno);
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Can't call variable from other class

    Follow the Java coding conventions: variable and method names start with a lowercase letter. Your use of the name CombateObject for a variable has led to confusion. To us, it looks like a class name.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Quote Originally Posted by Norm View Post
    Yes the following will create an instance of the Combate class:
    Combate CombateObject = new Combate();

    Will that instance have the data that you want to access? Or is the data in another instance of the class?
    You need to get a reference to the instance that has the data that you want access to.

    Where is there a reference to an instance of the Combate class that has the data you want access to?
    Remember that "this" refers to the current instance of a class.


    If you are calling the ataque() method from the Combate class, you could pass the value of champno as a parameter in that call: ataque(champno);

    Ok, I passed the value of champno as a parameter with:

    Java Code:
    ataquesObject.ataque(champno);
    and that added a parameter to the Ataque class:

    Java Code:
    public void ataque(int champno2) {
    
    //....
    champno = champno2;
    
    }
    Is that going to work wel if I need to pass a lot of variables?


    Follow the Java coding conventions: variable and method names start with a lowercase letter. Your use of the name CombateObject for a variable has led to confusion. To us, it looks like a class name.
    Code Conventions for the Java Programming Language: Contents

    db
    Did that, but it didn't solved the problem.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Can't call variable from other class

    Quote Originally Posted by Rsmpt View Post
    Did that, but it didn't solved the problem.
    It's not supposed to solve the problem. It's to facilitate you getting better, more targeted help on this or any Java forum.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    if I need to pass a lot of variables?
    Pass a reference to the Combate class instance instead of a single variable.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Quote Originally Posted by DarrylBurke View Post
    It's not supposed to solve the problem. It's to facilitate you getting better, more targeted help on this or any Java forum.

    db
    Hum, thanks, I misunderstood what you said. I will try to, from now on, getting the code clearer.


    Pass a reference to the Combate class instance instead of a single variable.
    Can you please show me an example, because I didn't understand really well how can I pass a reference like you're saying...

    Thanks, Im making progress now. :)

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Can't call variable from other class

    Java Code:
    public void ataque(Combate aCombateRef) {
    This is how to receive a reference to the Combate class object.
    Use the this reference when calling the method.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Rsmpt is offline Member
    Join Date
    Apr 2012
    Posts
    30
    Rep Power
    0

    Default Re: Can't call variable from other class

    Quote Originally Posted by Norm View Post
    Java Code:
    public void ataque(Combate aCombateRef) {
    This is how to receive a reference to the Combate class object.
    Use the this reference when calling the method.
    Thanks, I think I got it. :D

Page 1 of 3 123 LastLast

Similar Threads

  1. Call a public string variable
    By onstock in forum New To Java
    Replies: 3
    Last Post: 11-23-2011, 12:25 PM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  4. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  5. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •