File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when private static var. is initalized?

 
Moya Green
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. public class practice {
2. private static int x;
3.
4. public static void main (String[] args)
5.{change(x);}
6.
7. public static void change (int x) {
8. x++;
9.}
10}
The program compiles and runs well. Why did the output print nothing?
I thought x was automatically initialized to 0 as a member variable.
Please help. Thank you in advance!
Moya
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Moya:
Where is your print statement?
 
Shishio San
Ranch Hand
Posts: 223
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barkat,
Thx for your explanation.
Wonder though why i get the following error when i try to compile the following using visual age for java version 4
final char c1 = 1;
byte s1 = c1;
Error: Type mismatch cannot convert from char to byte
and it works only when i apply the cast to c1.
byte s1 = (byte) c1;
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shishio:
Seems like that topics got mixed up here. Anyway, I did not get that error with JDK1.3.1_01.
Thanks
Barkat
[ September 03, 2002: Message edited by: Barkat Mardhani ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat, I have noticed this topic mixing too, one of my replies got put on the wrong thread. I have reported it on the JavaRanch thread.
-Barry
BTW I liked your analysis of Shishio's problem, but I cannot find it any more.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Moya, as Barkat said, where's your print statement?
This looks like it's going to be a a problem of trying to change a primitive type variable passed by value. Only objects passed by reference can be altered inside a member function. The post increment (++) is applied to a copy of the parameter not to the parameter itself.
-Barry
 
Shishio San
Ranch Hand
Posts: 223
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx Barkat,
Wonder why the thread is missing though.
 
Moya Green
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops! Sorry.......
The code is reposted as below
------------------------------
1. public class practice {
2. private static int x;
3.
4. public static void main (String[] args)
5. {
change(x);
6. System.out.println (x);
}
7. public static void change (int x) {
8. x++;
9.}
10}
-----------------------------
Barry, I understood that x is a primitive variable and its value would not be changed by the method change(). What do you think the default value of x at line 2? Shouldn't "0" be printed out at line 6? But in fact, nothing was printed out after code compilation and execution. Any idea what happened?
Any input is appreciated.
Moya
[ September 04, 2002: Message edited by: Moya Green ]
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends, I think line 6 should be inside the block of main().
Anyway, change has an parameter in its signature. This means only the local copy of x inside change will be incremented. When change() finishes and the function call stack returns to main() the original static variable will be unchanged.
You can test this by declaring change() to have no arguments. The incremented value of x should appear.
 
Moya Green
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony,
Thank you for correcting the code. I put the Sytem.out... in the main method.
Moya
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Moya:
The original code you posted i.e. :

This code prints 0 for me without making any changes.
Barry: Line 6 was already in main block.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
same here...it prints 0
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barkat said:
Barry: Line 6 was already in main block.

I never said that, Anthony said it. I would NEVER say that, honest
Anyway I reckon 0 will be printed.
-Barry
 
Moya Green
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
Double Thank you! When I ran this file again today, it printed out 0. I don't know what was wrong with me before. (Maybe I am a little bit careless.) Anyway, I appreciate all your help.

Moya
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Barry. I need boost of short term memory sinche while you are writing your piece, you can not see the original post.
Thanks
:roll:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic