I want to keep the JtextArea as global so that other function have global access to it. However with the code above this error exists:

"non-static variable outputArea cannot be referenced from a static context"

Also when I call the function buildOutput I get this error:

"non-static method buildOutput(int[][]) cannot be referenced from a static context"

I Know like I said the first program works but I really want to find out what is wrong with the second one so that I familiarize myself with Java syntax.

Any help is appreciated! Thanks.

02-04-2009, 01:05 AM

Fubarable

It will work if you make everything static including your outputArea variable and your buildOutput method. Having said that, don't get used to this. As you progress in Java, you will learn to avoid using static variables and static methods if at all possible as you can't use OOPs techniques with this.

ps: Here's to hoping that you'll do better with this post and respond to anyone who answers your questions. You didn't do this with your previous post. Good luck.

02-04-2009, 01:51 AM

RR_QQ

Ok thanks for the reply. Well I might not have replied if someone answered a question because I really don't like to post more comments and clutter stuff up if there really is no need to! And I will keep doing that same thing to keep things cleaner. But thanks for your advice.

However, here I'm still at a loss. I understand that I have to use "static". But you say I shouldn't use static. So how do I make the code in the second block work? This is code I got from a book but doesn't seem to work. Thanks!

02-04-2009, 02:01 AM

emceenugget

outputarea is an instance variable owned by a specific instance or object of your type, that is it isn't static and owned by the class. To access it, you need an instance by using the new operator and access that instance's outputarea:

Code:

MatrixManipulation mm = new MatrixManipulation();

and you access it via mm.outputarea, however it isn't good practice to access the fields of an object directly but to rather create methods to do this for you.

02-04-2009, 02:14 AM

RR_QQ

So in other words I programmed it wrong lol. Ok...will organize my code thanks.

02-04-2009, 02:20 AM

Fubarable

Quote:

Well I might not have replied if someone answered a question because I really don't like to post more comments and clutter stuff up if there really is no need to! And I will keep doing that same thing to keep things cleaner.

a simple thanks is always appreciated and never clutter.

Anyway, if I were doing this myself, I'd do what nugget suggests, try to code this as methods of a class, then in the main instantiate an object of the class and call the appropriate methods on the object. My guess is that you're quite new at this, and if so, a decent introduction to Java book or the Sun intro tutorials should help you out a lot here. Best of luck.