473,406 Members | 2,769 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,406 software developers and data experts.

static modifier

public class Test {

private static int a;

public static void main (String [] args)
{
modify(a);
System.out.println(a);
}

public static void modify(int a) {
a++;
}

}

Please tell me why this code prints 0 instead of 1?

Thanks in advance...
Jun 28 '08 #1
3 1467
Laharl
849 Expert 512MB
It does that because primitives (ints, floats, doubles, etc) are passed by value, which means that a copy of the variable with the same value is actually passed to the function. This local copy is destroyed after the function exits.
Jun 28 '08 #2
JosAH
11,448 Expert 8TB
It does that because primitives (ints, floats, doubles, etc) are passed by value, which means that a copy of the variable with the same value is actually passed to the function. This local copy is destroyed after the function exits.
ps. also *non-primitives* are passed by value; it's the only parameter passing
mechanism Java knows of. Those non-primitives are references which is just a
pooped up term for pointers.

kind regards,

Jos
Jun 28 '08 #3

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

Similar topics

2
by: BobReynolds | last post by:
We use Log4j for all our logging, I want to know why we have to do the following in EVERY class final private static org.apache.log4j.Logger log = LoggerUtil.getLogger(thisClassName.class); ...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
8
by: Ganesh Kundapur | last post by:
Hi all, struct test { static int x; }X; main() { X.x = 100; }
5
by: A.M | last post by:
Hi, I have i utility class contains static functions (and also members) I usually use in may applications. Can i have a static like constructor so any time the app starts, The constructor...
3
by: Sujala | last post by:
In Java, the Main method cannot have any other access modifier than 'public'. How is it that in C# you can have a private access modifier to the Main method ? Can anybody throw any light, comparing...
6
by: Sahil Malik | last post by:
Maybe I am missing something really elementry here - but why can a sealed class not be marked as static? (Even though it can contain all static members?) ... hmmmmm !! PS: This is .NET 2.0 -...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
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
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: 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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.