Results 1 to 4 of 4
  1. #1
    erthbound0 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Variables from another class won't assign to main class

    Hey guys. Simple program and my variables are not recognized in the main even after calling them for assignment. When I try to print out the assigned Health, Strength etc variables in Line 20 it says they are not assigned. What am i doing wrong?

    Java Code:
    package rpgmain;
    import java.util.Scanner;
    public class Main {
    
    	
    	public static void main(String args[]) {
    		System.out.println("Welcome to Earthbound!");
    		Scanner chooser = new Scanner(System.in);
    		System.out.println("Do you want to be a fighter (1) or mage (2)?");
    		int Charpick;
    		Charpick = chooser.nextInt();
    		if (Charpick == 1){
    			Fighter Fighter1 = new Fighter();
    			Fighter1.Fighterstats();
    			System.out.println("You have chosen: Fighter");
    		}else{
    			Mage Mage1 = new Mage();
    			Mage1.Magestats();
    			System.out.println("You have chose: Mage");
    			System.out.println("Health: "+ Health +" Magic: "+Magic+" Strength: "+Strength+" Heal: "+Heal);
    		}
    		
    	}
    
    }
    package rpgmain;
    
    public class Mage {
    	public void Magestats(){
    		int Health, Strength, Magic, Heal;
    		Health = 50;
    		Strength = 5;
    		Magic = 25;
    		Heal = 10;
    	}
    
    }
    package rpgmain;
    
    public class Fighter {
    	public void Fighterstats(){
    		int Strength, Health, Magic, Heal;
    		Strength = 20;
    		Health = 100;
    		Magic = 10;
    		Heal = 5;
    	}
    	
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Variables from another class won't assign to main class

    The compiler has no clue what those variables are, as they loose scope once the called methods exit. Suggested reading:
    Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    erthbound0 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: Variables from another class won't assign to main class

    dowhile thanks for the reply. I am still not clear after reading that site what to do. How do I bring the scope of the variables into the main?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Variables from another class won't assign to main class

    Quote Originally Posted by erthbound0 View Post
    dowhile thanks for the reply. I am still not clear after reading that site what to do. How do I bring the scope of the variables into the main?
    One answer is in the first example in the link: create instance variables/fields in your classes. In the first example in the link, this would be cadence, gear, etc...

Similar Threads

  1. Replies: 0
    Last Post: 10-23-2011, 10:49 PM
  2. Could not find or load main class BubbleSort.class
    By blaqkout in forum New To Java
    Replies: 5
    Last Post: 09-12-2011, 07:54 PM
  3. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  4. Replies: 2
    Last Post: 02-18-2010, 05:26 PM
  5. Replies: 1
    Last Post: 01-03-2010, 09:48 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
  •