aspose file tools*
The moose likes Beginning Java and the fly likes Why does this fail?  I don't quite understand. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does this fail?  I don Watch "Why does this fail?  I don New topic
Author

Why does this fail? I don't quite understand.

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515

I understand that I get
"Can't make a static reference to a non static variable".
But I'm still not getting the gist of this.
------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
main is a static method. It is a class level method and does not relate to any particular INSTANCE of the class.
However your variable MyArg is an instance variable. Each time you create a new Arg object it will have it's own MyArg. \
The static method does not know WHICH instance's MyArg you are talking about, so it complains.
To do this you need to MAKE an Arg and the use the variable for that one instance in the static method.
Arg x = new Arg();
x.MyArg=argv;
System.out.println(x.MyArg[1]);
Now it knows which one to look at.

[This message has been edited by Cindy Glass (edited May 07, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Or, of course you could make the variable a class variable so that it also does not relate to any particular instance of the class by making it static also. Then the static method CAN access it.
PS Do your realize that the first thing in the array is at MyArg[0] not [1]?
[This message has been edited by Cindy Glass (edited May 07, 2001).]
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Yes I did know that Cindy. Thank you for the reminder though Thank you for the helpful information as well.
------------------
What's this H2SO4 doing in my fridge?? ( thud )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does this fail? I don't quite understand.