• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

methods and what they mean to me

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<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).]
 
Laura Holcombe
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why not
 
Greg Harris
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


"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).]
reply
    Bookmark Topic Watch Topic
  • New Topic