Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting a static instance to a variable

 
Lisa Foster
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am passing the argument from main for example
intvalue is global and static

public static intvalue;
public int newint = intvalue;
but when I pass this newint to another class int is always null???

intvalue = Integer.parseInt(args[0]):
help thanks Lisa
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does this even compile?
public static intvalue;
 
Lisa Foster
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I meant
public static int intvalue;//global
problem is if I decalre it with a value
such as
public static int intvalue = 99
IT will pass 9 to the class without problems but I need to do it from main from the args array ,.
Thanks Lisa
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that the "intvalue = Integer.parseInt(args[0]):" is throwing a null pointer exception, right? It's either not picking up your command line argument, or something is spelled incorrectly. Try to print out args[0] just prior to that statement above and see what you get.
 
davidlong
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lisa
You will never be able to pass the value, you want, to another class because you did not pass the right value to your newint variable.
The problems was that you pass "intvalue" to "newint" too early that it had recieved "intvalue" initialization value and unable to change it in later code. you should know that declaring newint=intvalue never make "newint" automatically change with "intvalue". The declaration was just passing the init value of "intvalue" to "newint", this is why you got always zero, or 99 when you changed init value.
To solve your problem, you may try assign newint right after;
intvalue = Integer.parseInt(args[0]);
newint=intvalue;
By the way, why do you not directly pass intvalue to another classes, it is static and pass it should be more easy.
David
 
Paul Smiley
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right on David! I missed that one. What she tried to do would have worked with objects (both pointing to the same object) but not with primitives. Good catch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic