• 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

illegal static declaration in inner class, modifier 'static' is only allowed in constant variable de

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm new here on this forum and I would like some help if it is possible. I'm trying to resolve a problem in one exercise in java. When I'm trying to compile in netbeans show me this error " illegal static declaration in inner class, modifier' static' s only allowed in constant variable declarations". Anyone can helpying me resolve the problem. In Bold is where shows me the error. The exercise is:


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to the Ranch!

Here is a summary of the important parts of your class (the parts which matter to the questions:

You have a static variable inside a non-static inner class. The class Empregado does not actually have a static context since it exists only in the context of an instance of Empregado_irs. So you can't use static variables there. You need to ask yourself how that static variable is supposed to behave. You could have many instances of Empregado_irs, and in each one have multiple instances of Empregado. In this case, how should the value of ultimoNumEmp increment? For example, look at this:

Say you have the following objects:


The value of eirs1_e1.numero should be 1 because it is the first one made. But what shout the value of eirs2_e1.numero be? It is the first Empregado instance in the second instance of Empregado_irs? Should it have a value of 1 (counting is for the number of instances of Empregado per Empregado_irs) or should the value be 4 (counting is consistent across instances of the outer class)?

If you need the count to be for the number of instances of Empregado per Empregado_irs, then you would make the ultimoNumEmp variable an instance variable of Empregado_irs:


If you need the count to be consistent across all instances of Emprgado_irs, then the variable should be a static member of the Empregado_irs class:
 
reply
    Bookmark Topic Watch Topic
  • New Topic