• Post Reply Bookmark Topic Watch Topic
  • New Topic

static factory method  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HelloWorld.getName() returns null.  new HelloWorld().getName() returns Tom.    How to  get name value using HelloWorld.getName() ?


 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you made the name field static?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assign the appropriate value to the static member variable 'name' in line 4, or add a static initializer block to assign the desired value to 'name'.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think the problem is that the name field and its get method are static. If you remove the static modifier, that problem will disappear. There are other problems in the code, of course.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Regarding the original question...

kri shan wrote:HelloWorld.getName() returns null.  new HelloWorld().getName() returns Tom. 


Simply, one code path sets the variable, and the other doesn't. I highly recommend debugging it to see where -- as that is the best way to figure these types of questions out anyway.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!