simple method problem :(
If I have an array in a class file and am trying to access the array from a seperate method in that class file..
Let's say I have a class blahblah.
int[] blah = new int[5];
int[] blah2 = new int[5];
void fillBlah(){
blah[0] = 1;
blah2[0] = 2;
// or loop through and fill all elements.. etc..
}
And outside of the class file in my main class - i call the fillBlah method - it gives me a non-static error. Essentially what I'm trying to do is to just create a method to fill the elements of an array from another class.
[594 byte] By [
johno24] at [2007-11-20 11:56:27]

# 1 Re: simple method problem :(
The question isn't clear enough to answer. You start be saying you want to access the array from a separate method in the same class, then finish by saying you want to fill the array from another class...
If you're getting an error, please post up the full text of the error message, and if you can explain clearly what you're trying to achieve, and post the relevant code (in [CODE]..[/CODE] tags), it would help.
The greatest mistake you can make is to be continually fearing you will make one...
E. Hubbard
dlorde at 2007-11-10 2:13:59 >

# 2 Re: simple method problem :(
Ok sorry.. Let's say I have my main class.
public class mainBlah{
public static void main(String[] args){
// do blah
blah.doBlah();
}
}
From there, I am trying to call a method from another class blah that will allow me to populate an array.
class blah{
int[] array1 = new int[2];
int[] array2= new int[2];
public void doBlah(){
array1[0] = 1;
array1[1] = 2;
array2[0] = 3;
array2[1] = 4;
}
}
So the error I get from the main file is:
non-static method doBlah() cannot be referenced from a static context
blah.doBlah();
I hope this explains it better.
# 3 Re: simple method problem :(
There is a plethora of google links for 'cannot be referenced from a static context'. But to summarize:
You can't call a non-static method from a static method.
mainBlah.main() is static.
blah.doBlah() is not.
Really, there were TONS of google links though. Have ya heard of google?
# 4 Re: simple method problem :(
There is a plethora of google links for 'cannot be referenced from a static context'. But to summarize:
You can't call a non-static method from a static method.
mainBlah.main() is static.
blah.doBlah() is not.
Really, there were TONS of google links though. Have ya heard of google?
This is a programming forum is it not? Here for people to ask questions right? I'm sure google has the answer to every question if you look hard enough - but I'd rather address it with a person. Deal with it and don't be so elitest about it. In anycase, I got it work - thank you. But relax, it's that sort of attitude that keeps new people to programming away from asking questions.
# 5 Re: simple method problem :(
This is a programming forum is it not? Here for people to ask questions right?Yes we are a programming forum but we're not a teaching Java forum. Your question was a very basic question which we will generally answer especially if it appears that the poster is making a reasonable effort themselves but unfortunately for you, at this time of year, we get inundated with requests from students who can't be bothered to do any research themselves and so these sort of questions become a little tiresome hence Martin O's response.
I'm sure google has the answer to every question if you look hard enough - but I'd rather address it with a person.Your first port of call should always be reference material such as books, google etc and only then, if you can't find the info or don't understand what you've found, post on a forum. Remember we give our time and experience for free, we are not paid to sit here and answer every request and why should we give up our precious time answering a question just to save someone 15 minutes of research. We are more than happy to help people when they have run out of options but not when they would rather someone else do their research for them.
keang at 2007-11-10 2:17:54 >

# 6 Re: simple method problem :(
But relax, it's that sort of attitude that keeps new people to programming away from asking questions.
You mean all I have to do is be a little mean to you & then you wont post any more 'cant-be-bothered-to-put-in-effort' questions? Wow...that does make me feel relaxed, thanks.
# 7 Re: simple method problem :(
... Deal with it and don't be so elitest about it.It's worth bearing in mind that you're talking to the people who will decide whether to answer you in future... If you pay for a service, you're entitled to be demanding. When you're asking for help it pays to be less testy. Anyone can (and should) use a search engine, but there are very few programmers who are willing to make an effort to help novices, so the least you can do is make an effort yourself and not treat them like a lazy man's alternative to Google.
In anycase, I got it work - thank you. But relax, it's that sort of attitude that keeps new people to programming away from asking questions.Perhaps, but it's your kind of question and attitude is going to keep new people to programming from getting answers. But don't let these responses put you off asking. We welcome sensible questions from people who've made an effort and are still stuck.
The truth is, when all is said and done, one does not teach a subject, one teaches a student how to learn it. Teaching may look like administering a dose, but even a dose must be worked on by the body if it is to cure. Each individual must cure his or her own ignorance...
J. Barzun
dlorde at 2007-11-10 2:20:03 >

