Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7943819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:23:01+00:00 2026-06-04T00:23:01+00:00

I am wondering do all the local variables become static if we declare them

  • 0

I am wondering do all the local variables become static if we declare them in a static method ?

for example:

  public static void A(){
        int x [] = {3,2};
        changeX(x);

        for (int i = 0; i< x.length; i++){
             System.out.println(x[i]);   // this will print -1 and 1
        }
  }
  private static void changeX(int[] x){
        x[0] = -1;
        x[1] =  1;
  }

As far as I understand that Java is pass by value always, but why the state of X has changed after we made the changeX call ? Can anyone explain that please ? and can anyone explains how does Java deal with static variables in terms of memory allocation ? and what happen if we pass a static variable to a function as a parameter (I know people won’t normally do that)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:23:02+00:00Added an answer on June 4, 2026 at 12:23 am

    The answer to most of your questions is “the same as any other variable.”

    Local variables in static methods are just local variables in a static method. They’re not static, and they’re not special in any way.

    Static variables are held in memory attached to the corresponding Class objects; any objects referenced by static reference variables just live in the regular heap.

    When you pass a static variable to a method as an argument… absolutely nothing interesting happens.

    Regarding the scenario in your code:

    1. Imagine that you have a toy balloon on a string (the balloon is your array object, and the string is the reference to it declared in A().)
    2. Now you tie another string on to the balloon and hand that string to a friend (that’s exactly what happens when you call the changeX() method: the string is the parameter of the method, and it points to the same object.)
    3. Next, your friend pulls in the string, takes a black marker and draws a face on the balloon (this is like the changeX() method modifying the array).
    4. Then your friend unties his string, leaving just your string attached to the balloon (the method returns, and the local variable in changeX() goes out of scope.)
    5. Finally you reel in the string and look at the balloon: of course, you see the face (your A() routine sees the changed array.)

    It’s really as simple as that!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering what happen if you declare a local thread within a method?
I was wondering if you could show me all the various ways to declare
I was wondering how I could create component-scoped beans, or so-to-say, local variables inside
I'm wondering if there is a way to print out all accessible variables in
I was wondering if it is possible to push all my local commits (all
Hi all I was wondering how did the packages org.ietf , org.omg , org.w3c
Wondering if it is at all possible. found: <script type=javascript> isSafari3 = false; if(window.devicePixelRatio)
Hullo all, Wondering if there are any Java hackers who can clue me in
Hey all. I'm wondering how to implement a timeout event in the System.Windows.Forms.Webbrowser control?
Hey all, I am wondering what the best way to do the following is:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.