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 6 of 6

Thread: Static keyword for arraylist containing objects

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Static keyword for arraylist containing objects

    Hey guys, I've come across something that i'm not overall sure about regarding the static keyword in Java.

    I'm making a vertical scrolling game where the player simply shoots enemies and they shoot back as they fall, dropping items if they die such as power ups and coins. I have an enemy called Bat and this is the bullet creation code in the update method:

    		if(oldPlayerY + 220 > posY && getBulletDelay > 0.90f){ 
    			batBullets.add(new Bullet(posX + 10, posY - 10));
    			getBulletDelay = 0;
    		}
    This code basically adds a delay for when the bat can shoot as well as what range it will start shooting at. This is fine, but here is where I hit the problem.

    The method is creating a new bullet object and it then adds that to the arraylist called batBullets, which is simple enough. I then need to access this arraylist in the main game update class so I can render those bullets on the screen, even if the bat dies. I was always taught that you use the static keyword when you need to access something from the class that doesn't require an object. Because of this, I have the following code.

        	for(Bullet bullet : Bat.batBullets){
    			bullet.setY(bullet.getY - 5); // Set the bullet to fall
    			renderMap.getSpriteBatch().draw(bullet.batBullet(), bullet.getX(), bullet.getY()); // render the bullets
    		}

    This seems perfectly fine to me because I need to access the batBullet arraylist and it doesn't make sense to create a new bat object as I already have random spawning in place for them. Can someone just clear this up for me?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Static keyword for arraylist containing objects

    I was always taught that you use the static keyword when you need to access something from the class that doesn't require an object.
    I see the usefulness of this point, but I would disagree with your interpretation and application of it. You haven't shown enough code for me to give what I would consider a better approach, so I'm willing to accept your approach for this question . . .
    Can someone just clear this up for me?
    . . . however, I don't understand what you need cleared up. It seems all is well with your approach, so what's your question or what do you need cleared up?

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Static keyword for arraylist containing objects

    Sorry, I should of explained this a little better.

    I basically just wanted to know if the static keyword for the arraylist was the correct thing to do in this situation. It doesn't make sense to create more bat objects just to get the bullets from the arraylist and this is what I wasn't sure about.

    The static keyword has always been something that threw me. I'll look for some examples on the correct usage and hopefully this will help me clear things up

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Static keyword for arraylist containing objects

    Making sure I understand:

    1. Enemies are Bat objects
    2. Any number of Bat objects could exist at a time
    3. Bat object shoot bullets
    4. Bat objects never run out of bullets

    If the above is correct, I'm not sure why you need an ArrayList of bullets at all, certainly not one tied to each Bat object or one for all Bat objects. When shooting, just create a Bullet object and shoot it.

    However, this answer doesn't clear up your question about the static keyword. One of the discussions that helped me - still helps me when I get confused - is Oracle's discussion about class members. Remember, the alternative to "class variable" is "instance variable" which means a variable created for the exclusive use of each instance. A "class variable" is a single variable for the whole class that is SHARED by all instances of the class and/or doesn't require an instance of the class to access at all.

    Hope this helps.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    SauronWatchesYou (June 14th, 2014)

  6. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Static keyword for arraylist containing objects

    I would discourage the use of static in the way you describe.
    It goes against the OOP principles that java is build on.
    Instead, try to have a "Game State" class or something like that. This Game-State manages bats and bullets and is known to each of them.
    When a bat creates a bullet it will tell the Game-State that a bullet was created and the Game-State will add it to a list.

    Then you can render all bats and bullets that exist in the Game-State.

  7. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    GregBrannon (June 14th, 2014), SauronWatchesYou (June 14th, 2014)

  8. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Static keyword for arraylist containing objects

    Thank you both I understand what the issue is now and I'm reading the link that Greg posted which has helped a lot.

    I see the purpose of the Game-State and how that is more built around OOP design of Java and this actually helps me out with some other features of my game I was planning. I'll get to work on it right now, thanks again guys !

    --- Update ---

    Just an update of how I got on with the static issue.

    I made the GameState class that manages the bullets and bats, then I simply made my gameState object in the main loop. I'm passing the gameState into the parameters that need it such as creating the bullets and adding them to the list and everything seems to be working as intended. I'm pretty sure i'm doing it right anyway it's more OOP like now at least, without any static variables/methods as they aren't required for what I need.

    Thanks for the help guys

Similar Threads

  1. Searching ArrayList<Objects> for a name?
    By javacloud in forum Loops & Control Statements
    Replies: 5
    Last Post: October 23rd, 2013, 09:43 AM
  2. Ask objects with parameters in Arraylist
    By sabbath in forum Collections and Generics
    Replies: 5
    Last Post: April 7th, 2013, 07:26 AM
  3. Bubblesort of an ArrayList with objects
    By bondage in forum Algorithms & Recursion
    Replies: 9
    Last Post: January 4th, 2012, 11:51 AM
  4. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 01:20 PM
  5. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM