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 9125401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:47:44+00:00 2026-06-17T06:47:44+00:00

I’m having some trouble understanding pretty basic Java code, I can’t figure out how

  • 0

I’m having some trouble understanding pretty basic Java code, I can’t figure out how in the end of compiling x=2. Because through my logic it should be 4. The code itself:

public class eksami_harjutused {

public static int x=2;
   public static int y=2;  

   public static void main(String[] args) {     
     foo(bar(foo(x)));      
     System.out.println("main x,y: "+x+" "+y);
   }  

   public static int foo(int x) {     
     x++;
     y++;   
     System.out.println("foo x,y: "+x+" "+y);
     return x;
   }

   public static int bar(int x) {
     int z=0, y=10, u=0;    
     --y;
     for(y=1; y<(x*x); y++) {
       for(z=1; z<x; z++) { 
         u++;
       }
     }
     System.out.println("bar x,y: "+x+" "+y);
     return z;       
   }           
}

It prints out:

foo x,y: 3 3

bar x,y: 3 9

foo x,y: 4 4

main x,y: 2 4

  • 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-17T06:47:46+00:00Added an answer on June 17, 2026 at 6:47 am

    Well, x is passed by value – since it is int type, so any modification to x in the callee functions will not affect x in the caller function. You can think of giving a copy of value in x to the callee, and the callee can do whatever with it without affecting the x in the scope of the caller.

    Passing by value is done for all the primitive types in Java. And passing by reference is done for the rest (Object – note that array is Object).

    Another thing is the effect of variable shadowing in foo and bar methods: x is declared as parameter to foo and bar, so the class member x is shadowed. Any access to x in foo and bar methods will refer to the argument passed in, not the class member x.

    The value of x printed in the main method is from the class member x, which is never touched during the execution of the program.

    In contrast, you can see the variable y modified twice in 2 calls to the foo method, since y in foo method will refer to the class member y. The y in bar method, however, refer to the local variable y declared in the bar method.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.