• 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

static methods in detail

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 3 questions related to the following program:-

My question starts from the eight'th line ....
1.if we cannot access non static variables why does the static method main lets me make a variable x?
2.i get it that i cannot make an instance variable of S1 but then why can i make an instance variable of Y?

I am sorry if the questions are too stupid but i think it's best to have answers for the language you love.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code won't compile at all, so it's hard to answer your questions... There is an extra '}' at the end, and you don't initialize 'x'.

If you correct those two errors, it compiles and runs just fine.

and for question 1, the answer is because it is not a non-static variable.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please keep your indentation when pasting code next time. I've added some and it immediately shows that class S1 is a non-static nested class inside class Xyz. I think Fred missed that (because of the lack of indentation), because there is no extra }.

budsy remo wrote:1.if we cannot access non static variables why does the static method main lets me make a variable x?


You can't access non static fields. Local variables declared inside the method itself are always accessible inside the method.

2.i get it that i cannot make an instance variable of S1 but then why can i make an instance variable of Y?


Class Y is a top-level class. Class S1 is, as I said before, a non-static nested class inside class Xyz (a.k.a. an inner class). Because it's nested the same rules for fields apply - you need an instance to create instances of this class:
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup...totally missed it was an inner-class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For detailed background information, see: Understanding Instance and Class Members.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a million Rob Spoor ,didn't know that we can actually make instances of an inner class through that syntax . Thanks to others too for answering .Will give some more study .
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic