Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tracy Le
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tracy Le
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i emphasis for same thing in my very first reply to the post
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic