• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Language fundamental

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take a look at the code below
public class example {
int i[] = {0};
public static void main(String args[]) {
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]) {
i[0] = 2;
i[0] *= 2;
}
}

1.How can you define another integer array with the same name in the method.
Is it becoz the method is static so the non static code of the class is inaccessible to static part ??
2.why is the o/p 4 ?? Is it becoz a copy of the array reference is passed and not the value of i[0] ??
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand ur first question but the o/p is 4 becos u are printing the i[0].which is 4 returned by ur method which has
i[0]=2
i[0]*=2(i.e 4)
And also i[]={0} means ur storing 0 in the array i
 
Shilpa Bhargava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about the ambiguity !! in simple words...can u define the same variable at the instance level as well as locally in a method ??
in this case
int[] i has been declared both inside and outside a method !!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is allowed and it does not relate to a static vs. instance situation.
You'll eventually see a constructor or method similar to this:In this example, instances of the class Foo are defined to have a private int member called number. This member is assigned a value from the constructor that has an automatic variable of type int also called number. To explicitly state when the instance member is to be used rather than the automatic variable, number is prefixed with this. .
Note that when using a member data item inside of any method (or constructor or initializer block), even though the programmer won't usually prefix its reference with this. when no local variable has the same name, the this. prefix is considered to be implied and I think the compiler does actually fully qualify each instance reference with the prefix this. .
So, um... is any of this making sense?
To answer your second question, take a look at Corey McGlone's flash animation on this very subject.
 
Shilpa Bhargava
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dirk,
It did clarify my doubt !!
[ September 24, 2002: Message edited by: Shilpa Bhargava ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic