Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static method Causes problem

I have a static method as following:

public static void writeArticle(TypeA typeA, TypeB typeB) {
  AWriter writer =  AFactory.getWriter("aWriter");
  Article article = writer.newArticle();

  /* PARAMETER WRITE START */

  article.set("title", typeA.getTitle());
  article.set("author", typeB.getName());
  article.set("age", typeB.getAge());
  // …
  /* more set statments here */
  writer.write(article);
}

Could this method cause a problem that the writer will write a value-mixed Article? That is, when 2 class (Class A and ClassB) instances calling this method, will Article get some of typeA values from ClassA and some from ClassB?

like image 403
sarahTheButterFly Avatar asked Sep 02 '26 11:09

sarahTheButterFly


1 Answers

No. Why do you think the arguments from two different calls would get mixed up? There's no reason to think they would.

If this is a multi-threaded program, you should ofcourse be careful with sharing objects between threads; if those objects have mutable state (member variables that can be changed) you should take care that two threads are not modifying the state at the same time.

like image 152
Jesper Avatar answered Sep 04 '26 00:09

Jesper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!