• 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

Error: The blank final field customerRepository may not have been initialized

 
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got error The blank final field customerRepository may not have been   initialized


Full code:


Uploaded it on github to make it easier for you to see the problem:
Github
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A final field must be definitely assigned (once only) by the time the constructor completes. Since the constructor in that class doesn't assign to that field, the class won't compile. What you wrote in line 25 is a method, not a constructor, because its name isn't the same as the name of the class.
By the way: always start ClassNames With CapitalLetters.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not even a method since there is no return type.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a method, but it is ill‑formed because of its lack of return type; that will produce another compiler error.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to use that logic then it could be argued that it is a constructor, it just has an ill-formed name.

It's NOT a constructor because it doesn't have the same name as the class.
It's NOT a method because it doesn't have a declared return type.

The intent was clearly to make a constructor, not a method.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . The intent was clearly to make a constructor . . .

All right then. I agree about the intent. But the compiler won't see that; it will see a method with missing return type.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right about which way the compiler would lean and it makes sense to do it that way.
 
martin codey
Ranch Hand
Posts: 90
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . The intent was clearly to make a constructor . . .

All right then. I agree about the intent. But the compiler won't see that; it will see a method with missing return type.



I understand that it might  be a illformed constructor and I quite honest don't completely understand everything I do. But if you look at my other git repository it worked completely fine. Github second repository
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The name of the class in line 20 (It should have started N not n) and the name of what you intended to be a constructor in line 25 are different. A constructor must have exactly the same name as the class and no return type. You can get serious errors if you mistakenly mark a constructor void because the javac tool won't issue any errors or warnings.

martin codey wrote:. . . don't completely understand everything I do.

In which case you should ask for explanations until you do understand what you are doing.

But if you look at my other git repository it worked completely fine. . . .

In which case, please let git or a diff program find the differences.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic