Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Computing Items in anArray List from another method in the same class

 
Imad Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my title explained abit of what I want done!


So Ive got files put into an array



And then I got files put into another array



Then how do I listFiles() the files in both arrays as if they were comming from one folder?
Becasue I want to compute all files from the arrays, to see which ones are the same size in bytes.




I dont know how the mehtod can view the arrays from its scope



 
Imad Ali
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and the arrays dont have specifed lengths, they couuld have any amount fo files in them
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this a problem? If an array is declared a class variable, as opposed to a method variable, then it should not be a problem to access that array from within a method in the same class. The only thing I can think might be a problem is if the method is static but the class variable (array) is not.

If you have declared the arraylist as a method variable, ask yourself if there is a reason why it can't be declared a class variable. That's one way to deal with it.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't say "class variable" unless you mean "static field". You probably would do better to use the proper jargon: "instance field." That would remove any confusion about whether it is static or not.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please don't say "class variable" unless you mean "static field". You probably would do better to use the proper jargon: "instance field." That would remove any confusion about whether it is static or not.


Duly noted. I should use the right words, that is indeed important. Anyways, my point was not so much the type of variable, but where it is defined, in the class as opposed to in the method, but you are right.

Anyways, not to quibble, but Sun uses the terminology class variables and static fields as meaning the same thing, associated with the class. And they tend to use the term instance variable for the other kind. I assume that's ok here.

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

regards.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance field? Instance variable? Agree: what's the difference. And as I said "class variable" = static field. I don't think we disagree, just I am feeling pedantic today.

Actually there is a reason for being precise with jargon: it helps other professionals understand you with economy of words. Two words "static" and "field" and everybody knows what you mean.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic