• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

static and non-static variable

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a dum question, but I could not figure it out. Please shed a light.
Why are get str: null and test2Str: null, if I use non-static for private String str2; in class Test2.

Only If I use static, the string will not be null. This occurs in my project, I cannot use static. Please help.




the output as below:

set string: this is a test2
get str2: null
test2Str: null



--------------------------------------------------------------




-------------------------------------------------------------------------


 
Ranch Hand
Posts: 211
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that your methods is Test and Test3 are each creating their own Test2 objects and references. The string value from the Test2 object in the Test3 object is never set so it has the default value of null.

It might help if you rename all your classes and methods, as it is its extremely confusing for anyone.
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
My question is. If I use static, I can get the string value, but I cannot use static.
I know I did not init the string and will get null. I got stuck with static and non-static.
Thanks again.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kee Kee moon wrote:If I use static, I can get the string value, but I cannot use static. I know I did not init the string and will get null. I got stuck with static and non-static.


I think you're confusing yourself with all that talk about static and non-static.

As Tyson said, the string is never set in Test2 instance used in Test3, so it is null. That has nothing at all to do with whether that field is static or non-static, the result would be the same in either case.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:
As Tyson said, the string is never set in Test2 instance used in Test3, so it is null. That has nothing at all to do with whether that field is static or non-static, the result would be the same in either case.



No, if str2 is static the output is always "this is a test2". A new object being initialized won't change the current static value back to the default value. To further answer OP's question, a static modifier means that when the value changes it changes for all instances of that object.

If you want to ensure that all objects have a non-static value that is the same, you have to iterate through every object and check/set that value.
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tyson Lindner wrote:

Ulf Dittmer wrote:
As Tyson said, the string is never set in Test2 instance used in Test3, so it is null. That has nothing at all to do with whether that field is static or non-static, the result would be the same in either case.



No, if str2 is static the output is always "this is a test2". A new object being initialized won't change the current static value back to the default value. To further answer OP's question, a static modifier means that when the value changes it changes for all instances of that object.

If you want to ensure that all objects have a non-static value that is the same, you have to iterate through every object and check/set that value.



Thank you.
I cannot use static, but I need to get the value. Can you give me some examples how to work it out. Thanks again.
My friend and I have been debated about this, in the beginning I was clear about this. As now, I am really confused and feel dumb.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kee Kee moon wrote:
I cannot use static, but I need to get the value.



I'm not sure what you mean by "get" the value. If one object has a value, for another object to "get" that value (if its not static) you have to manually pass that value to that object. So generally that value must be passed to your new object in the form of a constructor or method.

Again, it will help clarify what you are trying to accomplish or demonstrate if you redesign your code.
 
reply
    Bookmark Topic Watch Topic
  • New Topic