• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with using variable before it is defined  RSS feed

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does the below code compile fine when the name is used in constructor before it is declared?


 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because the compiler is smart enough to figure out that the variable is actually defined.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:because the compiler is smart enough to figure out that the variable is actually defined.


Then why does the below code doesn't compile giving error something like that j1 is not defined at this point


 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Local variables need to be defined before they are used. But in your first case it was an instance variable. All instance variables are created before the constructor runs, so any method can refer to any of them regardless of the order they appear in the file. The order lines are executed does not necessarily match the order they appear.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought you can see that the variable is not only not initialised. It is not even declared.
As well as the problem about using instance variables in a static method.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And do not correct code you have posted to match comments made later. You would appear to have added static, which makes Matthew’s post look mistaken.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the explanation still stands - things declared in the class body will be declared before any method is run.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And do not correct code you have posted to match comments made later. You would appear to have added static, which makes Matthew’s post look mistaken.

I didn't edited my code when i edited the post,i had wrongly used [code] tag , so i just corrected it and that too within 30 seconds after making that post and not after Matthew posted.


Have a look at the time when Matthew posted and also when i edited the code.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd probably already started writing the reply by then, so didn't see the update.

But don't worry about it
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about my mistake.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!