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

  • Home
  • SEARCH
  • 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 6875255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:17:57+00:00 2026-05-27T04:17:57+00:00

Im using a list of objects (Area) in java, which i defined as so:

  • 0

Im using a list of objects (Area) in java, which i defined as so:

    public static List<Area> areaList = new ArrayList<Area>();

I add content to my list as so:

    areaList.add(new Area(px, py, pz, radius, wl));

then i access the list, to check each of the Area’s within it like so:

for (int i = 1; i < areaList.size(); i++) {
    System.out.println(areaList.get(i).somevariable));
}

(ignore the ‘somevariable’ and i also didnt use println(), this was for example, the way i accessed using:

areaList.get(i)

is whats important here)

But it returns all the objects inside the List as having the same values – that of the last one accessed.

Can anyone tell me where im going wrong?

  • 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-05-27T04:17:58+00:00Added an answer on May 27, 2026 at 4:17 am

    I looked through your uploaded code and found the error.

    You declared every field in the class Area as static:

    public class Area {
        public static int posx;
        public static int posy;
        public static int posz;
    }
    

    The fields are therefore equals for all instances, because there is just one posx field for the class Area (read this question and the answers for details). Because of this every write to, e. g., posx overrides the old value. Remove the static keyword and then all instances of the class Area have their own instance variable.

    You should also think about making them private and provide getter and setter methods.

    I haven’t looked in detail through your code, but there are several places where you used static variables. You should revisit this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I using a genric list(m_equipmentList ) which is collection of objects (Schedule_Payitem). How can
I'm using a list of objects as the data source of my GridView and
How do I load library objects by string list using Flash and AS3? I
How can I list all instantiated objects in all application, using FASTMM4 or default
I'm using asp.net mvc2 and trying to send a list of json objects with
I'm .net newbie starting one project for fun. I'm using list box filled with
I am using a List Control to display a representation of elements within a
I need to provide my users a list of choices from a model which
I was using newInstance() in a sort-of performance-critical area of my code. The method
I'm using ASP.NET 3.5 to build a website. One area of the website shows

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.