Results 1 to 11 of 11
  1. #1
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Problem accessing variables in other objects

    Being very new to Java, I'm trying to get the hang of using objects. I'm trying to access the variables "x" and "y" in an instance(named Mob1) of the class "General_Mob".


    The "Runner" class
    Java Code:
    public class Runner {
    
    	public static void main(String[] args) {
    		System.out.println("Check.  Check.  Check.");
    		General_Mob Mob1 = new General_Mob();
    		Mob1.Create();
    		System.out.print("X:" + Mob1 x + " Y:" + Mob1.y);
    		
    
    	}
    The "General_Mob" class
    Java Code:
    public class General_Mob {
    		public static int x;
    		public static int y;
    	/**
    	 * @param args
    	 */
    	public static void Create() {
    		
    		x = 0;
    		y = 0;
    
    	}
    
    }
    I am getting the following error:
    Java Code:
     Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Syntax error on token "x", delete this token
    
    	at Runner.main(Runner.java:19)
    How can I properly access the "x" and "y" variables from Mob1?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem accessing variables in other objects

    You're missing a period on line 7 above in your Runner class (but line 19 in your actual code it seems).

    It will behoove you to always scrupulously check the line that the error message refers to.

  3. #3
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Problem accessing variables in other objects

    Thank you!

    But now, I seem to be having a different problem. I'm also trying to run that same code in a separate method, but I'm getting the following error.
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	Mob1 cannot be resolved to a variable
    	Mob1 cannot be resolved to a variable
    Here is my modified source code for the "Runner" class:
    [code]
    Java Code:
    public class Runner {
    	public static void main(String[] args) {
    		System.out.println("Check.  Check.  Check.");
    		General_Mob Mob1 = new General_Mob();
    		Mob1.Create();
    		goAround();
    		
    
    	}
    	
    	public static void goAround(){
    		System.out.print("X:" + Mob1.x + " Y:" + Mob1.y);
    		
    	}
    
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem accessing variables in other objects

    You've got a scope issue: You are creating a Mob1 variable inside of the main method, but since this variable was declared inside of main, it is visible only in main method. In the goAround method, it is out of scope. You have two possible solutions:
    • Declare the variable in the class itself, or
    • Allow goAround to accept a General_Mob1 parameter and pass a reference to the object into the goAround method.

  5. #5
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Problem accessing variables in other objects

    Quote Originally Posted by Fubarable View Post
    You've got a scope issue: You are creating a Mob1 variable inside of the main method, but since this variable was declared inside of main, it is visible only in main method. In the goAround method, it is out of scope. You have two possible solutions:
    • Declare the variable in the class itself, or
    • Allow goAround to accept a General_Mob1 parameter and pass a reference to the object into the goAround method.
    Because goAround is going to be repeatedly looped later(if you can't tell, it's going to be part of a game engine), I can't declare it inside the loop; otherwise, it would constantly create new instances of General_Mob with the same name, leading to major errors(or atleast, that's my hypothesis).

    How can I allow goAround to accept the parameter?

  6. #6
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Problem accessing variables in other objects

    Many apologies for bumping this threat, but I really need an answer, please. Thanks in advance.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem accessing variables in other objects

    Quote Originally Posted by falconfetus8 View Post
    Because goAround is going to be repeatedly looped later(if you can't tell, it's going to be part of a game engine), I can't declare it inside the loop; otherwise, it would constantly create new instances of General_Mob with the same name, leading to major errors(or atleast, that's my hypothesis).

    How can I allow goAround to accept the parameter?
    This doesn't make sense to me, such as what you mean by creating new instances of General_Mob -- this is nothing that we've recommended. Please clarify greatly.

  8. #8
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Problem accessing variables in other objects

    If I were to declare Mob1 in the goAround() method, then wouldn't it repeatedly declare Mob1 as a new object every time it's looped?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem accessing variables in other objects

    Quote Originally Posted by falconfetus8 View Post
    If I were to declare Mob1 in the goAround() method, then wouldn't it repeatedly declare Mob1 as a new object every time it's looped?
    No one is telling you to declare Mob1 in goAround. Instead I was saying that one way to solve your problem is to pass it in goAround as a parameter.

  10. #10
    falconfetus8 is offline Member
    Join Date
    Nov 2011
    Posts
    14
    Rep Power
    0

    Default Re: Problem accessing variables in other objects

    How would I do that, then? I'm sorry, but I don't quite yet understand.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem accessing variables in other objects

    I'm not sure what is confusing you. Are you familiar with creating methods that accept parameters? If not, you'll want to review a methods tutorial or your book. If you are, then please be as specific as possible with your questions so we can give specific answers, else this will mainly be an unsatisfactory exercise in you asking general questions and me referring you to the tutorials.

Similar Threads

  1. Accessing variables from another class
    By tti123 in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 04:43 AM
  2. Inheritance - accessing private variables
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 04-26-2010, 01:21 AM
  3. Accessing Variables From a Different Class?
    By Sterling in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 05:01 PM
  4. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  5. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 04:36 AM

Posting Permissions

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