Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems with multidimensional arrays and global variables

1 view
Skip to first unread message

Michael Kragh Pedersen

unread,
Feb 11, 2005, 3:22:45 PM2/11/05
to
Well, I'm not good at Java, learning as I'm going, so this is getting me
gray-haired :(

First of all, I have a variable, which I want to use several places in
my methods, so I declare it just after the class. That should work, I
guess, but no, I get the error "non-static variable a cannot be
referenced from a static context". I don't get this...yes, both my
method and main are static, because I got errors about them not being
before. So what am I supposed to do to resolve this?

Further more, I want to declare a two dimensional array as global as
well, the only problem is, I won't know the size of that, until I'm in
the main method, therefore I want to redimension it there. But how
exactly do you do that? As it is now, the dimensioning line is in the
main method, and it gives a "cannot resolve symbol" error, which I'm
guessing is due to the scope of the array.

Michael K. P.

Carl

unread,
Feb 11, 2005, 3:48:11 PM2/11/05
to
Michael Kragh Pedersen wrote:
> Well, I'm not good at Java, learning as I'm going, so this is getting me
> gray-haired :(
>
> First of all, I have a variable, which I want to use several places in
> my methods, so I declare it just after the class. That should work, I
> guess, but no, I get the error "non-static variable a cannot be
> referenced from a static context". I don't get this...yes, both my
> method and main are static, because I got errors about them not being
> before. So what am I supposed to do to resolve this?
>

I think you need to take the time to fully understand the the concept of
static before you begin using it. Additionally, I think you should move
away from doing all your process inside the main() method.

In short, static members do not belong to an object. You do not need to
create an object to access the members. The main() method must be static
since it will be the first method called and there will not yet have
been a chance for you to create any objects. IMO, the main method should
then proceed to operate on OBJECTS, unless you have a good reason for
using static.

A simple class example would be as follows:
// code -->
public class Foo {

// this can be accessed by any of Foo's methods
private int someVariable;

// This gets called when a new Foo object is created.
public Foo(){
// do initial setup
}

public void runProgram(){
// start doing your processing
}

public static void main(String[] args) {
// process args[]

Foo foo = new Foo(); // create an instance of the Foo class.
foo.runProgram(); // start running the Foo class.
}
}
// <-- end code

> Further more, I want to declare a two dimensional array as global as
> well, the only problem is, I won't know the size of that, until I'm in
> the main method, therefore I want to redimension it there. But how
> exactly do you do that? As it is now, the dimensioning line is in the
> main method, and it gives a "cannot resolve symbol" error, which I'm
> guessing is due to the scope of the array.
>

This should most likely be handled in the constructor or a helper method.

// code -->
protected void setSomethingsSize(int x, int y){
this.someArray = new String[x][y];
}
// <-- end code

Hth,
Carl.

Michael Kragh Pedersen

unread,
Feb 11, 2005, 4:05:51 PM2/11/05
to

Well, I removed the static from my method, so that it's only the main
method, that is static, but it still gives the same error.

As for the array, I use this line

int p[][] = new int[n+1][3];

Isn't that what you mean by constructor? Unfortunately, that's only in
the main method, what I'm not sure of is how to dimension the variable
before the main method, and then redimension it in the main method.

Hal Rosser

unread,
Feb 11, 2005, 4:11:15 PM2/11/05
to

"Michael Kragh Pedersen" <m_kragh_...@hotmail.com> wrote in message
news:cuj3ul$8ac$2...@news.net.uni-c.dk...

ooo ok - keep in mind there's much more to learn - but to use that variable
in a static method, make it static - or just declare it it in the method.

Message has been deleted
Message has been deleted

Michael Kragh Pedersen

unread,
Feb 11, 2005, 4:23:53 PM2/11/05
to
Damnit, that came into the wrong thread...please look at the thread from
Hal Rosser.

Michael Kragh Pedersen

unread,
Feb 11, 2005, 4:24:26 PM2/11/05
to
Well, btw, my main method (which is static) calls my method, and then I
get the "non-static method" error. So I set my method for static. But
then my variable a, which I use in both main and my method, makes the
"non-static variable" error, so I set that as static as well. Now it
works fine, but is it really necessary to make everything static?

Michael Kragh Pedersen

unread,
Feb 11, 2005, 4:27:13 PM2/11/05
to
Well, btw, my main method is, of course static, but when I call my
method from main, I get the "non-static method" error, So I set my
method as static. But then I get the "non-static variable" error,
because of a variable, I use in both main and my method. So I set my
variable as static as well, and now it works fine. But is it really
necessary so set everything to static?

Michael K. P.

Patricia Shanahan

unread,
Feb 11, 2005, 4:32:16 PM2/11/05
to
Michael Kragh Pedersen wrote:
> Well, btw, my main method (which is static) calls my method, and then I
> get the "non-static method" error. So I set my method for static. But
> then my variable a, which I use in both main and my method, makes the
> "non-static variable" error, so I set that as static as well. Now it
> works fine, but is it really necessary to make everything static?

In your required-to-be-static main method you can create an
instance of your class and call its non-static methods:

SomeClass s = new SomeClass();
s.someMethod();

Patricia

Anthony Borla

unread,
Feb 11, 2005, 4:40:38 PM2/11/05
to

"Michael Kragh Pedersen" <m_kragh_...@hotmail.com> wrote in message
news:cuj3ul$8ac$2...@news.net.uni-c.dk...
> Well, I'm not good at Java, learning as I'm going, so this is
> getting me gray-haired :(
>

If you take the time to read through a tutorial then your hair colour will
not change [assuming it's not already grey ;) !]. Try:

http://java.sun.com/docs/books/tutorial/java/index.html

>
> First of all, I have a variable, which I want to use several places in
> my methods, so I declare it just after the class.
>

Like so ?

public class Test
{
int aVar;
...
}

>
> That should work, I guess, but no, I get the error "non-static
> variable a cannot be referenced from a static context". I don't
> get this...yes, both my method and main are static, because I
> got errors about them not being before. So what am I supposed
> to do to resolve this?
>

public class Test
{
int aVar;
...


public static void main(String[] args)
{

Test t = new Test();

t.aVar = 5;

System.out.println("aVar = " + t.aVar);
...
}
}

>
> Further more, I want to declare a two dimensional array as global as
>

Java does not support global variables. To obtain a similar effect you need
to declare any such variables as static items within a class, and access
them via the class. Example:

class Globals
{
public static int SOME_CONSTANT = 55;
}

To use in your code:

...
System.out.println(Globals.SOME_CONSTANT);
...

>
> well, the only problem is, I won't know the size of that, until I'm in
> the main method, therefore I want to redimension it there. But how
> exactly do you do that? As it is now, the dimensioning line is in the
> main method, and it gives a "cannot resolve symbol" error, which I'm
> guessing is due to the scope of the array.
>

Arrays, in Java, are objects, so it is a simple matter of allocating them
when required, using required specifications [but they can't, later, be
resized]. Also, many other data structures [known as 'collections'] exist
for more specialised data storage / handling.

I believe your Java learning interests are best served by completing the
tutorial I mentioned earlier as it covers most of the essentials you need to
begin effectively using Java.

I hope this helps.

Anthony Borla


Michael Kragh Pedersen

unread,
Feb 11, 2005, 5:03:36 PM2/11/05
to
Do you mean the class, that is defined at the start or what? Like said
in the start, I'm pretty much a newbie, so I'm not really following...

Michael K. P.

Michael Kragh Pedersen

unread,
Feb 11, 2005, 5:05:37 PM2/11/05
to
Yeah, something like that...but I don't declare the class again (what's
the purpose of that?). Anyways, I solved it by making the variable and
the method static, although it seems strange, that it's needed.

As for the arrays, well, they will be empty, until I need to redimension
them in the main method, I don't know if that does any difference?
Anyway, I will look at the tutorial then, didn't know, there was such an
extensive online tutorial...thought it was only in books :)

Michael K. P.

Patricia Shanahan

unread,
Feb 11, 2005, 5:46:04 PM2/11/05
to
Michael Kragh Pedersen wrote:

Not tested, so there may be typos:

public class SomeClass{


public static void main(String[] args){

SomeClass s = new SomeClass();
s.someMethod();
}

public void someMethod(){
// do the real work here, and in
// methods someMethod calls.
}
}

someMethod, being a non-static method, can access non-static
data and call other non-static methods in SomeClass.

Patricia


Don Freeman

unread,
Feb 11, 2005, 6:02:58 PM2/11/05
to
<top posting seemed more appropriate for this post>.

Being a Java newbie and having been struggling with the basic concepts of
the structure of a java program (as well as "static") I would like to say
that this is the best example I have seen so far of the basic form of a java
program. Nice, simple and after reading it I think I finally understand
what is going on. I have read far too many books and examples that seem to
enjoy obfuscating the concept they are trying to explain with way too many
extraneous details.

Thank you.
Don Freeman

If this group has a FAQ I nominate the following for entry:

"Carl" <_Nospam_@_DO_NOT_.USE> wrote in message
news:fS8Pd.3241$lz5....@newssvr24.news.prodigy.net...

blm...@myrealbox.com

unread,
Feb 12, 2005, 4:10:46 PM2/12/05
to
In article <cuj9vg$9h8$3...@news.net.uni-c.dk>,

I'll second the recommendation that you look at a tutorial/intro --
you seem moderately unclear on basic concepts, and some text written
for newbies ought to help with that. With regard to your specific
questions, here's a URL in the Sun tutorial that seems like it ought
to help with your questions:

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

--
| B. L. Massingill
| ObDisclaimer: I don't speak for my employers; they return the favor.

0 new messages