-1

I want to create multiple objects inside while loop and access all objects outside in JAVA 8. Currently using a list to store the obects, but all objects get replaced by one last object (last created).

I have tried initializing list inside try, outside try, nothing works.

Here is my test1.java,

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class test1 {
public static void main(String[] args){
    try {
        List<test2> objList=new ArrayList<>();
        BufferedReader encReader = new BufferedReader(new FileReader("./asd.txt"));
        String eachLine;
        while ((eachLine = encReader.readLine()) != null) {
            String[] data = eachLine.split("\\|");
            if(true){
                objList.add(new test2(data[0], data[1]));
            }
        }   // While ends here

        objList.forEach(x -> x.printEncLoc());

    }catch (IOException e) {
        e.printStackTrace();
    }
}
}

Here is my test2.java,

public class test2 {
private static String s1;
private static String s2;
test2(String s1new, String s2new){
    s1=s1new;
    s2=s2new;
}
public static void printEncLoc(){
    System.out.println("s1:"+s1+" s2:"+s2);
}
}

Here is my input file example (asd.txt)

hello|123
qwe|klj

It calls only the last object's printEncLoc function each time in the forEach line. It prints output as follows.

s1:qwe s2:klj
s1:qwe s2:klj

What is the problem here?

12
  • "It calls only the last objects function " -- sorry, you will need to explain what you mean here more explicitly. What do you mean by "last object" and "function"? Commented Jul 29, 2019 at 7:13
  • last object is the object of the class EncodeLocation created for last line of the file (which is assigned to eachLine). Function is printEncLoc function inside the EncodeLocation class. Commented Jul 29, 2019 at 7:17
  • 1
    Please reduce this to a minimal reproducible example and show sample input data and corresponding output. Commented Jul 29, 2019 at 7:19
  • 2
    I think the code outputs same lines both times is because you are using static variables ... in class test2, make everything non-static (variables and method) and then it will print out different lines Commented Jul 29, 2019 at 7:48
  • 1
    Unrelated: class names in Java go UpperCamelCase. So dont start them with a lowercase letter! Commented Jul 29, 2019 at 8:06

1 Answer 1

1

You made the properties in test2 static, this means all instances share the same properties. So when you change them for the 2nd row, the 1st row changes as well.

Remove the 'static' from s1 and s2, and from your printEncLoc() method and your code works.

EDIT: See https://www.baeldung.com/java-static for more on how static works

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.