Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: State Variables interaction with outside classes?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [SOLVED] State Variables interaction with outside classes?

    Hey everyone, i'm working on a Java application and have ran into a bit of a snag due to my lack of knowledge...

    I currently have 4 Java classes.

    #1 Has the main thread and creates a new instance of my game and calls the startGame method.
    #2 Has the Game class, methods, state vars, constructor, etc.
    #3 Has the class Monster which serves as a ?superclass? to the different monsters in my game. This class contains state variables that all monsters need. The constructor for this class sets all the vars to 0 or null depending on their type.
    #4 Is a monster that extends Monster. The default constructor sets it's actualName to "XXXXX" while another constructor allows for it to set a nickName while still setting actualName to "XXXXX".

    When #4 tries to use the variables set as private in the Monster class I get an error saying those are private. But the #4 class extends Monster...

    Shouldn't #4 have access to the Monster variables since it extends Monster? Setting the Monster state vars as public would have bad side effects on my game I think...


    I need suggestions of how to tackle this problem... Thanks folks.
    Last edited by Ace Coder; October 25th, 2010 at 09:20 AM. Reason: SOLVED


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,371
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: State Variables interaction with outside classes?

    See Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects) Extended classes do not have access to privates, so either provide protected/public access to them, or mark them as protected so children have access to them

  3. The Following User Says Thank You to copeg For This Useful Post:

    Ace Coder (October 24th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: State Variables interaction with outside classes?

    Thank you very much! You have no idea how much that link helped.

Similar Threads

  1. Interaction with external application
    By righi in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 2nd, 2010, 09:37 AM
  2. variables and efficiency
    By catkinq in forum Java Theory & Questions
    Replies: 3
    Last Post: February 7th, 2010, 06:09 AM
  3. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  4. Replies: 6
    Last Post: August 30th, 2009, 05:31 AM
  5. How to link two classes in java to use it method
    By Sterzerkmode in forum Object Oriented Programming
    Replies: 3
    Last Post: May 13th, 2009, 07:52 AM

Tags for this Thread