• Post Reply Bookmark Topic Watch Topic
  • New Topic

May not have been initialized?  RSS feed

 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. I'm just confused with a different approach with a kind of the same things (those looks the same for me). Here is a peace of the code:


Salmon is just a separate class with two fields and one test foo method which does nothing.
While this code is being compiled I'm getting the error message the variable rd may not have been initialized.

Settings it = null resolves the problem.

The question is - why should I explicitly set this pointer to 'null' while I shouldn't do the same with pointer of Salmon type?

Thank you very much.
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ha ha, I understand the reason just after I pressed "Submit" button, lol. It should be initialized because this value have to be returned in any case. Thanks everybody
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am surprised that would compile; please always show us the real code, including the return statement which produces the error.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: don't talk about pointers in Java. You aren't writing C code now.
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:By the way: don't talk about pointers in Java. You aren't writing C code now.


Ok, so how should I call it? "Variable"?
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When it comes to objects, we dont use 'variable'. 'reference' is preferred.

We say, the reference to Salmon Class is set to null.

Now, the 'reference' sal holds an 'instance' of the Salmon Class.
 
You Gin
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler goes through every path before a local variable is used; if there is any possibility of an unitialised value being used, it fails to compile. This is to prevent the value previously on the stack from being used by mistake.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The compiler goes through every path before a local variable is used; if there is any possibility of an unitialised value being used, it fails to compile. This is to prevent the value previously on the stack from being used by mistake.

Everytime a method is called, a new stack for it is created to hold local values,right? So, why is there going to be any previous values at all? Please correct me if i were wrong about that new stack thing.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote: . . . Everytime a method is called, a new stack for it is created to hold local values,right? . . .
Don't know. It might not be in all implementations of the JVM. You will have to see how much you can understand from the Java Virtual Machine specification section (and 3.6), but it does not appear that a new empty stackis created for each method call. Not certain, but it seems there are stack frames in side the stack, and it doesn't say that old values are deleted, or anything.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!