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

Instance variable instantiation in Constructor

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If Instance variable are being initialized before constructor then can somebody please explain why I get
//Cannot refer to an instance field id while explicitly invoking a constructor// error

 
Marshal
Posts: 73760
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are supposed to pass information to the constructor from outside. What you have is a field which hasn't been initialised (not “instantiation”) and you are trying to apply its own non‑value to itself.
This is correct syntax:-...but probably incorrect logic and design.
 
onygen nari
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Campbell Ritchie, but why the id field hasn't been initialized,
I mean are not Instance variables have default values and are usually initialized before the constructor
 
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
99_999 out of 100_000 Java developers have never seen that error, because they would never try to do that.

It is a weird, utterly and totally useless thing to do, that could literally never do anything good for you.  At best you would end up with the same value that your definition and instance initializers left it with before any of your constructors ran.

Apparently Java considers this and makes it a compile failure, slightly interesting.

You are correct about the order of instance initialization, which few people not preparing for a certification exam care about in much detail.

https://www.geeksforgeeks.org/instance-control-flow-in-java/

There are other weird trivia questions about order of initialization of static and instance members, also usually only paid attention to by people preparing for a certification exam.

Are you preparing for a certification exam or just very curious?

Also, Welcome to the Ranch!
 
onygen nari
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jesse Silverman, Yes I am preparing for Oracle Exam. however. my goal is to learn the language in detailed/logical way.

So rather than saying, this thing is allowed or not, I wanted to understand how things work under the hood.

for that reason I was wondering about the instance variable order and why it was not working.

 
Jesse Silverman
Bartender
Posts: 1059
33
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, there are definitely questions on the cert exams expecting us to know about this topic in some detail.

Each of the statements I made are true, I believe.
Campbell's example showed the most common example of how the this() call would be used.

I know for sure the Enthuware and I believe also the Sybex materials correctly describe the instance initialization process in excruciating detail, I only looked quickly but the link I provided seemed to as well.

I believe it is consistent in that both all of the in-line initializations as well as any instance initializer blocks will have completed by the time the first line of any constructor code runs.

It was interesting that you discovered a new syntax error in Java that I never saw before in my life.

As I said, I believe the error is because that code would never need to be written / would never do anything useful for you, so it is concluded to be a mistake.

Not because you were mistaken in your beliefs about the order of the steps in the initialization sequence, which you were not.
 
Campbell Ritchie
Marshal
Posts: 73760
332
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . same value that your definition and instance initializers . . . Java . . . makes it a compile failure . . .

You are applying a circular definition to the value of the variable, and the javac tool is programmed to look for circularities when you use constructor invocations.
 
What a show! What atmosphere! What fun! What a tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic