This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes non-static variable totals cannot be referenced from a static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "non-static variable totals cannot be referenced from a static context" Watch "non-static variable totals cannot be referenced from a static context" New topic
Author

non-static variable totals cannot be referenced from a static context

Tracy Le
Greenhorn

Joined: Nov 09, 2012
Posts: 2
Hello all! I am trying to compile my program but I keep getting this error: non-static variable "totals" cannot be referenced from a static context... its coming from this line :

edit: solved removing code
Amruta Mistry
Greenhorn

Joined: Jan 25, 2012
Posts: 9

The method which you are talking is static while the varibale you are tying to use within the same is non static.
Have you tried declaring the variable as static??


Thank You
Amruta Mistry
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Welcome to the Ranch. Please see your private messages for an administrative matter from JavaRanch.

Why did you make the showTotals() method static (line 55)?

Do you understand what static means? See this tutorial: Understanding Instance and Class Members


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Tracy Le
Greenhorn

Joined: Nov 09, 2012
Posts: 2
Hi Jesper,

I did as asked in the private message, sorry about that.

The program I am working on requires the showTotals to be static. I was able to fix that compiling error, but now my totals are not showing up correctly.

Invididual pleges should show up, but on the bottom it should show the grand total of all pledges for each student...and that is not working for me. Below is an example of the output and updated code.

edit: solved removing code.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Tracy Le wrote: . . . The program I am working on requires the showTotals to be static. I was able to fix that compiling error, but now my totals are not showing up correctly. . . .
That is not a compiler error. That is a design error. If you have marked something static because it compiles like that, there is a 99.9% probability that you have a serious design error about what is static and what isn’t.
You cannot correct a
non-static variable totals cannot be referenced from a static context
… error by making totals static. You correct it by making wherever it is referenced from non-static.
Amruta Mistry
Greenhorn

Joined: Jan 25, 2012
Posts: 9

I think i emphasis for same thing in my very first reply to the post
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Amruta Mistry wrote:I think i emphasis for same thing in my very first reply to the post
Not at all. You suggested worsening the mistake by making the variable static. Many people think the compiler error is suggesting that, but the correct solution is exactly the opposite.
 
 
subject: non-static variable totals cannot be referenced from a static context