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.
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.
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.
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.
Michael K. P.
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
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 K. P.
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.
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
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...
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.