aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when private static var. is initalized? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when private static var. is initalized?" Watch "when private static var. is initalized?" New topic
Author

when private static var. is initalized?

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
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

Joined: Aug 05, 2002
Posts: 787
Hi Moya:
Where is your print statement?
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
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;


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 29, 2002
Posts: 223
Thx Barkat,
Wonder why the thread is missing though.
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Jan 24, 2002
Posts: 49
Anthony,
Thank you for correcting the code. I put the Sytem.out... in the main method.
Moya
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
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

Joined: Apr 29, 2002
Posts: 200
same here...it prints 0


Be Afraid...Be very Afraid...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Jan 24, 2002
Posts: 49
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

Joined: Aug 05, 2002
Posts: 787
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
 
subject: when private static var. is initalized?