File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes methods and what they mean to me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "methods and what they mean to me" Watch "methods and what they mean to me" New topic
Author

methods and what they mean to me

Laura Holcombe
Greenhorn

Joined: Jan 07, 2001
Posts: 8
I have been working on this longer than I like to admit and I just can�t get it right.
I am trying to call a method and keep getting the error �Can�t make static reference to nonstatic variable in class Grains�. My code looks similar to this
Import somePackage.* ;
finding the class is part of the assignment, so I removed the package name from your post, Laura -- Marilyn

Public class Grains
{
someMethod numberGrains = new someMethod( �1� ) ;
public static void main( String args[] )
{
someMethod.someFunction( numberGrains ) ;
}
}

I think I have a grasp on the difference between static and object methods but I just can�t figure out how to define numberGrains so that it will work. So I guess I don�t totally understand static, but I have tried. I know the answer is staring at me just waiting to get out, but as of yet I just don't get it. Any ideas?
laura

[This message has been edited by Marilyn deQueiroz (edited July 22, 2001).]
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
laura, i hate to sound so cryptic, but i cannot give away too much... there is another way to initialize the initial value of your variable. look at the API for SomeClass and look closely near the constructors... also, you might want to try declaring your variable inside main()... or make your variable "static."


what?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
<pre>import somePackage.* ;

public class Grains
{
someMethod numberGrains = new someMethod( �1� ) ;
</pre>

This is actually
SomeClass numberGrains = new SomeClass( "1" ) ;
Right?
<pre>public static void main( String args[] )
{
someMethod.someFunction( numberGrains ) ;
}
}
</pre>


"Can�t make static reference to nonstatic variable in class Grains�. Read "Can't make static reference from static method main() to nonstatic variable numberGrains in class Grains."

[This message has been edited by Marilyn deQueiroz (edited July 22, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Laura Holcombe
Greenhorn

Joined: Jan 07, 2001
Posts: 8
Marilyn,
Sorry for the mis-naming. It just shows how new this stuff is to me. When reading your re-phrasing, I took the "Can't make static reference from static method main()... to mean that I need to create another method to call someMethod from. This produced the following compiler message "Can't make static reference to method java.somePackage in class java.somePackage". (I have gotten this message a lot recently). No luck there.
Greg,
I tried making the variable static; no luck. I got the following message "Can't make static reference to method java.somePackage in class java.somePackage". I also tried declaring the variable inside main() and received the same error message. I looked at the 6 constructors in someClass and found one that may work. But I get compiler messages "Identifier expected" for the following code.
someClass ( String 1 );
Can I just give up now? I feel like I take three steps back for every step forward, but I haven't taken any steps forward yet!
laura
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

OR

The second way is not recommended.
[This message has been edited by Marilyn deQueiroz (edited July 22, 2001).]
[This message has been edited by Marilyn deQueiroz (edited July 25, 2001).]
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
And why not
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
laura,
marilyn's example is exactly what i was talking about... i did not know i could show you that much, or i would have posted the code.
as for initializing your variable, the way i am suggesting will not have SomeClass( "x" ) in it, but rather SomeClass.xxx. i am not sure that this really matters, but i got "pushed" towards it on my first nitpick. if you can get your code working with the example from marilyn, and using SomeClass( "x" ), then go ahead and submit it.
Laura Holcombe
Greenhorn

Joined: Jan 07, 2001
Posts: 8
I wish I could say that it all makes sense to me now, but I am still working on trying to understand the concept. I did get a clean compile, which gave me a momentary sense of accomplishment. But then I realized I am not getting the results I need. I will keep plugging along, trying various things. I am going to blame it on my mainframe mentality getting in the way of this new learning.
laura
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
"The second way is not recommended."
Is this a style question? (From the style guide: Try to not declare a variable until just before it is used unless it will impact the performance of the code.)
Or does it have to do with keeping variables as private as possible?
Some other reason?
Thanks,
Pauline
Laura Holcombe
Greenhorn

Joined: Jan 07, 2001
Posts: 8
Won't even try to answer why the second example is not preferred, but I got my code to work. I just looked at how some of the previous assignments worked and tried a few things and viola....I have a ton of grain on the 64th square. I sent the assignment in minutes ago to be nitpicked.
Thanks for the help, I really needed it.
laura
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12

"The second way is not recommended."
Is this a style question? (From the style guide: Try to not declare a variable until just before it is used unless it will impact the performance of the code.)
Yes

Or does it have to do with keeping variables as private as possible?
Yes

AND

The key is that variables here are to be used as attributes for a class. But in this case, there is no need for attributes. Rather than being an object attribute, this static variable looks more like a global variable.

[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: methods and what they mean to me