File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Language fundamental

 
Shilpa Bhargava
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • 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] ??
 
raju vegiraju
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 !!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dirk,
It did clarify my doubt !!
[ September 24, 2002: Message edited by: Shilpa Bhargava ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic