Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8581045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:57:22+00:00 2026-06-11T20:57:22+00:00

I’m still a little new to Java, and I’m quite confused with how objects

  • 0

I’m still a little new to Java, and I’m quite confused with how objects are supposed to be used. I know of

object object = new object();

but these have to be accessed manually.

say I wanted to create a particle effect. small white dots appear in the centere of the screen, and slowly move to an edge of the screen at a random angle. as they approach, they go faster. finally, they are removed if they go out of the view area.

if I wanted to do this with objects, I would need to be able to:

  • automatically make objects
  • collect data from every object (their X and Y co-ordinates)
    so that they can be rendered
  • have the objects update every frame so that they can increase speed
  • destroy the objects when they go out of bounds.

How would I automatically make/destroy objects, and take in data from all of them independent of how many objects are present?
Right now, my games (well, I’ve only ever made 1 in Java) are limited to using an array and a sort of cellular automata, but this won’t work for bigger games.

This might have been asked before or there might be a tutorial on this, but all I managed to find was manually making objects. I’m probably searching for the wrong thing, so sorry if this is the case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:57:23+00:00Added an answer on June 11, 2026 at 8:57 pm

    Creating Objects

    I’m not sure what you mean by “automatically create objects”, but this is what constructors are for. When you define a constructor, you can add all sorts of implementation and initialization details in there that will create your variable exactly how you want it to be created. See “Providing Constructors for your classes”. So say you have a point…

    public Point(int x, int y) {
        this.xPos = x;
        this.yPos = y;
        this.color = Color.BLUE;
        this.size = Constants.DEFAULT_POINT_SIZE;
        // ... etc.
    }
    

    Collecting Data from Objects

    Getting and setting information in these classes would be achieved through getters and setters (also known as accessors and mutators).

    public int getX() {
        return x;
    }
    
    public void setX(int x) {
        this.xPos = x;
    }
    

    To collect the data from all objects in existence, independently of how many there are, simply have them all added to a List of some kind. You could even add them to a private static ArrayList<T> in your constructor and access the ArrayList<T> statically for your information without another part of your program ever having to “know” how many there are.

    Incremental GUI Changes

    There are a couple ways to implement your frame-by-frame changes, but I prefer using a Timer and an ActionListener. This allows you to set your frame rate and, in the public void actionPerformed(ActionEvent) method to perform the actions you require. This is somewhat complicated for a beginner, so you will likely have to do a good bit of reading and testing. See “How to Write an Action Listener” and “How to Use Swing Timers” to get you started.

    Destroying Objects

    There are no destructors in Java as there are in languages like C++. Instead, there is a garbage collector which runs periodically (and sporadically) which destroys all objects it deems no longer have a reference pointing to them. Thus, if you want the garbage collector to pick up your objects when they go out of bounds, simply assign all references to them to null. While this won’t destroy the object immediately, it ensures that they will be “tagged” by the garbage collector on its next run through.

    As mentioned, System.gc() is a method to “invoke” the garbage collector. However, you should not assume that calling it will force the garbage collector to run. In the API, it says that this call “suggests” to the System that it should run the garbage collector. Ultimately, it is not reliable and garbage collection is generally outside of your scope of control.

    From the API:

    Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

    Also, as a side nit pick, be sure to capitalize the class name when you are creating objects. 🙂

    Object object = new Object();
    

    For OP’s specific purposes: If you are concerned about a game environment where you are “drawing everything that exists,” (and so you want to destroy the objects immediately to ensure that the game runs correctly), consider using a List or Set and populating that with the things you want to draw, then removing them as they go off the screen.


    References:

    • http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
    • http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html
    • http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html
    • http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
    • http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.htm
    • http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.