• Post Reply Bookmark Topic Watch Topic
  • New Topic

Preferential treatment to the order in which the variables are declared in java  RSS feed

 
Nirmit Dalal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i wanted to know when are the objects created ? when are they initialized? and how is show() getting called without any reference or "this" ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nirmit Dalal wrote:i wanted to know when are the objects created?

Whenever you call 'new', and whenever an object is created implicitly by auto-boxing or, in the case of Strings, by the use of literals. In the latter cases, the actual "when" is complicated somewhat by the use of caches, but it's really not important to you as a programmer.

when are they initialized?

It depends what you mean by "initialize".

and how is show() getting called without any reference or "this" ?

Because the 'this' is implicit. Personally, I tend to put it in on all my names, but it's not required.

For a full explanation you really need to read the JLS, but the fact of the matter is that 99% of the time, this stuff usually isn't important. Create objects; use them; let the Garbage Collector clean them up - it really is as simple as that. The rest: DontSweatIt.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only to write this. when you need to disambiguate a member of the object from a local variable. The two ints are instance fields, so they are implicitly members of the object, so they are implicitly in a “this” context. You will have to go through the Java Language Specification and see which is executed in which order. You may find it difficult to understand, but there is a definite order things happen in.

And what do you think the output will be?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . 99% of the time, this stuff . . . isn't important. . . .
But this is one of the remaining 1%. Because there is poor design (using fields without being sure whether they have been initialised in the constructor or not), the order affects the output.

So: no escape. You will have to read the JLS section I quoted.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But this is one of the remaining 1%. Because there is poor design (using fields without being sure whether they have been initialised in the constructor or not), the order affects the output.

Hmmm. Looks a bit like an example from an SCJP exam primer to me, not real life. Personally, I tend to think that problems like these result from people being introduced to this stuff far too early, and trying to be "clever", rather than programming in a simple, natural fashion.

But you may be right.

Winston
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . example from an SCJP exam primer to me, not real life. . . .
You mean if that had happened in real life we would have this thread in Jobs Discussion under the heading, “They sacked me for writing this code in a critical app”?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . an example from an SCJP exam primer . . .
You are probably right. Or it was something OP wrote under the one condition which justifies writing anything never mind how daft:-
I wonder what will happen if…
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!