• 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

The reason to cause non-static variable cannot be referenced from a static context

 
Ranch Hand
Posts: 65
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't the static variable named "ml" be assigned a reference value in the first place just like another static variable named "f"?
You can compare the two pictures.
The complete code is as follow.

can-t-compile.PNG
[Thumbnail for can-t-compile.PNG]
can-compile.PNG
[Thumbnail for can-compile.PNG]
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get a different error:

Java Compiler wrote:No enclosing instance of type MiniMusicPlayer is accessible. Must qualify the allocation with an enclosing instance of type MiniMusicPlayer (e.g. x.new A() where x is an instance of MiniMusicPlayer).


This has to do with the class MyDrawPanel being a nonstatic inner class.  As the error says, you need an instance of MiniMusicPlayer to instantiate ml.  So for instance, this works:
So why does it work in the method setUpGui()?  Because at that point in the code, you do have an instance of MiniMusicPlayer: it's called this.  These two lines: ... are semantically equivalent.
 
Crystal Zeng
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much.
 
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic