473,385 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Have a problem on static modifier

public class X {
2. private static int a;
3.
5. public static void main (String[] args) {
6. modify (a);
7. }
8.
9. public static void modify (int a) {
10. a++;
11. }

why it is printing '0" ..as per the question int a should get increased and print 1
I am not understanding this
Aug 7 '07 #1
6 1377
JosAH
11,448 Expert 8TB
public class X {
2. private static int a;
3.
5. public static void main (String[] args) {
6. modify (a);
7. }
8.
9. public static void modify (int a) {
10. a++;
11. }

why it is printing '0" ..as per the question int a should get increased and print 1
I am not understanding this
The value of the parameter 'a' is incremented, it had the same value as the
value of the private static int member with the same name. Because you didn't
explicitly initialize that static member it has the value zero.

Java passes parameters by value; always; no exception to the rule.

I don't understand why you think anything is printed at all.

kind regards,

Jos
Aug 7 '07 #2
praveen2gupta
201 100+
public class X {
2. private static int a;
3.
5. public static void main (String[] args) {
6. modify (a);
7. }
8.
9. public static void modify (int a) {
10. a++;
11. }

why it is printing '0" ..as per the question int a should get increased and print 1
I am not understanding this

Hi
a++ operator say the print first and then increment. So change your line no 10 as below program.
a is initilized to 0 and when class and static method are loaded with it's value is first loaded to 0 after that it is incremented so you are getting it 0.


Expand|Select|Wrap|Line Numbers
  1. public class X 
  2. {
  3. private static int a;
  4.  
  5. public static void main (String[] args) 
  6.     {
  7.     modify (a);
  8.     }
  9.  
  10.  public static void modify (int a) 
  11. {
  12.  System.out.println(++a);
  13.  }
  14. }
  15.  
Aug 7 '07 #3
r035198x
13,262 8TB
... and the static modifier is not the problem here.
Aug 7 '07 #4
JosAH
11,448 Expert 8TB
... and the static modifier is not the problem here.
As a matter of fact: if 'a' weren't a *static* member, the entire thing wouldn't even
compile.

kind regards,

Jos
Aug 7 '07 #5
r035198x
13,262 8TB
As a matter of fact: if 'a' weren't a *static* member, the entire thing wouldn't even
compile.

kind regards,

Jos
I know but the OP's problem of a not incrementing is really because of the passing by value (and possibly the hiding) rather than the static modifier. The title of the thread would give a different impression.
Aug 7 '07 #6
JosAH
11,448 Expert 8TB
I know but the OP's problem of a not incrementing is really because of the passing by value (and possibly the hiding) rather than the static modifier. The title of the thread would give a different impression.
Yep, as I already wrote in post #2 ...

kind regards,

Jos
Aug 7 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ganesh Kundapur | last post by:
Hi all, struct test { static int x; }X; main() { X.x = 100; }
4
by: Gerben van Loon | last post by:
Hello, I'm quite new to Csharp and like to know how I can call a function from an upper class. Following example might explain my problem a bit better: public class MyClass { public void...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
5
by: Andy | last post by:
Why can't I create an interface and make its implementation static/shared? Is there some pattern that lets me work around this? Any explanation of why this is would be appreciated. This...
8
by: Paw Pedersen | last post by:
Is there a way to save a variabel that can be access from a static method? I hope there would be some way to save it in memory so I don't have to save it in a file. It's only for a few minutes the...
2
by: Jason Huang | last post by:
Hi, What's the difference "public static" and "static"? Do we have "protected static" and "private static"? If we do, in what situation will we apply them? Thanks for help. Jason
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
1
by: Charlie | last post by:
Hi: I'm looking at some field declarations. Programmer has it static private int x. Shouldn't it be private static int x? Doesn't the access modifier come before variable modifier? But it...
2
by: ambikasd | last post by:
Hi, What if the static modifier is removed from the signature of the main method? thanks, ambikasd
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.