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

static variables

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question :
By changing which line in the following code can you make it compile with no compile - time errors .

class StatTest {

class Int {
static int x = 10 ; // ---> line 1
int y = 10 ; // ---> line 2
};

static class Inter {
static int x = 10 ; // ---> line 3
int y = 10 ; // ---> line 4
};
};

Options :

a . Remove static from line 1
b . Add static to the declaration on line 2
c . Remove static from line 3
d . Add static to the declaration on line 4

Answer : a.

what is the reason for this?

Can anyone help me out?

Thanks in advance..
regards,
rajani.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't declare a static field into a non-static inner class. The class itself has to be static, or you declare it in the top level class..

Greetz
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
non-static inner class cann't have static members
 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..

Thankyou for your replies..

regards,
rajani.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can define the static memebers in the non static inner class provided it is final.
so it should be
final static int x = 2;
reply
    Bookmark Topic Watch Topic
  • New Topic