• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

constructors and gc

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why i am getting the following error :-
Sample2.java:13: error: non-static variable x cannot be referenced from a static context
               System.out.println("x in main: " + x);
                                                  ^
Sample2.java:14: error: non-static variable y cannot be referenced from a static context
               System.out.println("y in main: " + y);
                                                  ^
2 errors

My code is:-


Is it related to the scope of the variable? If it is then 'x' & 'y' must have the scope till the main method!
 
Greenhorn
Posts: 7
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manish Pamnani wrote:Why i am getting the following error :-
Sample2.java:13: error: non-static variable x cannot be referenced from a static context
               System.out.println("x in main: " + x);
                                                  ^
Sample2.java:14: error: non-static variable y cannot be referenced from a static context
               System.out.println("y in main: " + y);
                                                  ^
2 errors

My code is:-


Is it related to the scope of the variable? If it is then 'x' & 'y' must have the scope till the main method!




you are violating the basic principle here:

from a static context/method, you can't access non static variables, you can access those through class instance/object.
 
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. I also think that is a bad error message. It misleads beginners into thinking there is something “normal” about things static. In fact, SM's correction moves the variable into an instance/object context. The error message would have been easier to understand as

Instance member XYZ can only be accessed in an instance context.

I trust you only the code to see what would happen; you have some design problems, including a main() method much too long and a variable which is never used.
 
Campbell Ritchie
Marshal
Posts: 75716
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, how did you choose the thread title? The discussion has nothing to do with constructors and GC.
 
Ranch Hand
Posts: 393
9
Open BSD BSD Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Manish Pamnani Main Is A Pain can be a useful read
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic