• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

what is the logic behind this

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai all,iam a new member to this group,but a long time reader of this group.... i have a small query,i need a little bit of explanation..hope u all will respond.....well
my query is in the first prg method(a) passes value i.e "a=0" and in the method a is incremented.well! when it prints, it prints 0.ie there is no change but in the second prg the changes made will reflect back and it prints "j=4".There is a small dout about this,if u could please tell me.

/*prg 1:***************************************************/
public class est
private static int a;
public static void main(String [] args) {
public static int modify(int a)
return a++;



public class est
private static int j = 0;
private static boolean methodB(int k)
j += k;
return true;

public static void methodA(int i)
boolean b;
b = i < 10 | methodB (4);
b = i < 10 || methodB (8);

public static void main (String args[])
methodA (0);
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sushi nair,

This is a problem with shadowing.

In the first program every time when you call the method modify() an integer variable is created with the same name as instance variable and instead of increment in instance variable the local variable gets incremented. The local variable value will be changed not the instance variable value.

In the second program there is no problem with shadowing. Because you didn't use the same variable name as instance variable. The increment will applied to the instance variable itself.

I like you because you always keep good, crunchy cereal in your pantry. This tiny ad agrees:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic