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 8649049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:27:48+00:00 2026-06-12T13:27:48+00:00

Hello i have some simplified code here showing the problem i have. Basically i

  • 0

Hello i have some simplified code here showing the problem i have. Basically i can not seem to add to the end of the ArrayList instead it writes over itself. Please help

Main

public class Main {

public static void main(String[] args) {
    HolderOfList h = new HolderOfList();
    h.addToHolder(new Num(2, "Bob"));
    h.addToHolder(new Num(3, "Cat"));
    h.addToHolder(new Num(4, "Dog"));
    h.printAll();
}
}

Class holding ArrayList

import java.util.ArrayList;
import java.util.List;

public class HolderOfList {
List<Num> num;

public HolderOfList() {
    num = new ArrayList<Num>();
}

void addToHolder(Num n) {
    num.add(n);
}

void printAll() {
    for (int i = 0; i < num.size(); i++) {
        System.out.println(num.get(i).getI() + num.get(i).getStr());
    }
}
}

Element held in the ArrayList

public class Num {

private static String str;
private static int i;

public Num(int i, String str) {
    this.str = str;
    this.i = i;
}

String getStr() {
    return str;
}

int getI() {
    return i;
}

}

The desired output is
2Bob
3Cat
4Dog

but all i get is

4Dog
4Dog
4Dog

I have a larger scale project with this problem in it any ideas?

Thank You in advance

  • 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-12T13:27:49+00:00Added an answer on June 12, 2026 at 1:27 pm

    Get rid of the static modifiers for your Num variables as they’re making it so that all Num instances effectively share the same single variable (not really as they’re actually variables of the class, but the behavior is the same). Make them instance (non-static) variables instead.

    In other words, change this:

    public class Num {
      private static String str;
      private static int i;
    

    to this:

    public class Num {
      private String str;
      private int i;
    

    Lesson learned: use the static modifier sparingly and only when it makes sense to do so. It doesn’t make sense here.

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

Sidebar

Related Questions

Hello i have some question about java. here is my code: public static void
i have some code here: -(void)handleButton { NSLog(@hello); } and i wanted to connect
Hello StackOverflow :) I have created some code inside my onStart(); method to ensure
Hello I have a problem with trimming string in c++. It adds some weird
I have some fairly generic code which uses preprocessor macros to add a certain
I have some HTML code that goes a little something like this. <p style=text-align:left;>Hello,
Hello I have some javascript code, a script included inside my page. I want
I have some code that I need to fix to print Hello World!. For
hello i have some problem with jquery val() when i click on div ,
Hello everyone I have some problem to grab movie title with regular expression. how

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.