• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 7126
185
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic