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

Creating instance of a non-static array method

 
Peter Taylor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone!

My problem is basically the following. I would like to get out data from a non-static array, so in order to do that I must make an instance of it. My problem is that I don't really now how to do that.

My code looks like the following, and it is obviously wrong:


I could solve this problem if I made Values static, but I cannot do that because Values array comes from a package, the source code of which I don't own.. So basically my question is about getting the value of a non-static array method. I know it's a very easy problem, but I would be really thankful if someone could guide me towards the solution to this basic problem. Thanks in advance!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Also note that, by convention, Values() should be values()--methods and variables start with a lowercase letter.

And finally, not that it's a big deal here, but still something to be aware of, every time you call Values() (that is, both iterations of your for loop), you're creating a new array. And the way you're doing it, you're only using one value from each array and ignoring the rest. The usual way to do it would be to call the method once, store the results in a variable, and then use that variable for the iteration.
 
Peter Taylor
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, I finally resolved this problem in the way you mentioned in the second half of your post. I've never found a site before, where people are this helpful with beginners, thanks again!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome! I'm glad it's clear now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic