Reputation: 23
Class Stat1.java
package Static;
import java.util.ArrayList;
import java.util.Locale.Category;
import java.util.TreeSet;
public class Stat1 {
static TreeSet<String> category= new TreeSet<>();;
public static void main(String[]args){
Stat1 s0 = new Stat1();
s0.category.add("Pocket Money");
s0.category.add("Salary");
Stat1 s = new Stat1();
s.category.add("Mother");
s.print_Category();
Stat1 s1 = new Stat1();
s1.category.add("Father");
s1.category.add("aaaaaaaa");
Stat1.category.add("999");
s1.print_Category();
}
public static void print_Category(){
System.out.println(category);
}
}
When I run the above class the output is-
[Mother, Pocket Money, Salary]
[999, Father, Mother, Pocket Money, Salary, aaaaaaaa]
Class Stat2.java
package Static;
import Static.Stat1;
public class Stat2 {
public static void main(String[]args){
Stat1 s = new Stat1();
s.category.add("brother");
System.out.println(s.category);
}
}
Output is:
[brother]
Now According to my understanding when we use static keyword then each object shares the same copy of that instance which is evident in Stat1.java class.But in Stat2.java the output shows only the String that was added to the TreeSet in that Class only.
According to me the output should consist of all the elements that are added to the TreeSet.
Please explain what is done wrong.
Thanks in Advance!
Upvotes: 1
Views: 147
Reputation: 10557
the output should consist of all the elements that are added to the TreeSet.
I'm assuming you expected the output of Stat1 and Stat2 to be like this:
$ java -cp . Stat1
[Mother, Pocket Money, Salary]
[999, Father, Mother, Pocket Money, Salary, aaaaaaaa]
$ java -cp . Stat2
[999, Father, Mother, Pocket Money, Salary, aaaaaaaa, brother]
^^^^^^^
"brother" included after running Stat1 then Stat2 |
The reason you're only seeing brother
after running Stat2 is because you are starting a brand new process. The static
keyword only affects how data is shared within a single process.
For instance, if you have a single java program in the file myprog.jar
, and you run it twice
java -jar myprog.jar &
java -jar myprog.jar
then each one of those commands starts a new process. Even though they are the same program running with the same classes, they do not share memory, so they do not share any instances of classes. Even if myprog.jar
uses static fields in classes, those classes have no idea about each other.
(So what does static
actually do for you? Here is an easy, example-driven description.)
Solution: If you want data to be shared across different processes, then you need to read and write to a database or a file so that the data lives after the process dies. This is called persistence.
Theory: If you want to know more about what databases and files are doing for your programs when they are used in this way, read about inter-process communicantion.
Advanced: If you want to use "the same" objects (i.e. objects with the same data) between different processes, learn about object relational mapping, which is a technology that automatically uses files/databases to synchronize objects across processes. But I suggest practicing more with java and with files before attempting any of that.
Upvotes: 4
Reputation: 106430
You're misunderstanding a few things here.
First, the static
keyword applies in several contexts, but let's focus on your case. If it's attached to a field, then the value of the field is shared among instances of the class. In other words, if you have multiple instances of a class, those instances will all reference the same field.
This is the scenario you exhibit in your first case; since you create more than one instance of Stat1
, each instance has the same TreeSet
, which is why you see more values added to it when you print the second time.
This is also why you're not seeing the same behavior in your second example. Since you've only created one instance of Stat1
, the static variable only holds the one value.
You're only executing one program at a time (which means you're only executing one of those cases at a time), so the behavior you were hoping to see in your second class won't materialize unless you instantiate more Stat1
classes inside of Stat2
.
Upvotes: 1
Reputation: 1049
When You run second class java doesn't know nothing about first.
If You write s.main(null);
in your second class all strings will be in the set.
Upvotes: 1