wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Instance Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance Variable" Watch "Instance Variable" New topic
Author

Instance Variable

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Hello Everyone
During programming i usually come across an error related to Instance Variable.
Compiler forces me to initialize the instance variable. For Example :

In above code, compiler force me to give some value to "x" , when i assign it "null" the error gets removed.
But its not that compiler force me every time, sometimes it allows me to simply define it like above code. So, can anybody tell me when it is necessary to define the instance variable & when its not ??

Thanks!!!
Regards
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Really? What is the exact error message that you get?

Instance variables do not need to be initialized explicitly. If you don't, they will be initialized with the default value, and what that is depends on the type. For int, it is 0.

You cannot assign null to an int, because int is a primitive type; null only works for reference types.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You are probably mistaking "instance variable" for just any variable.
Instance variables are part of the class and do not need to be explicitly initialized. Variables inside a method are not instance variables. They are called local variables.
They must be explicitly initialized before they are used. You can sometimes declare them without initializing them. But they must be definitely assigned before you can use them.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
E Armitage wrote:You are probably mistaking "instance variable" for just any variable.


Thanks Jesper & Armitage. Sorry that was my mistake in the question. You both have valid points. but believe me i have often come across such kind of errors...maybe they may have come due to different reasons. I know the concept of local variables, class variables & instance variables.. I dont have any such example to show you right now..Whenever in future i meet with it.. will post it here ..
Thanks Again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
E Armitage wrote: . . .
Instance variables are part of the class . . .
Part of the object, surely?

I am sure you are right and there is confusion between fields and local variables.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Campbell Ritchie wrote:
E Armitage wrote: . . .
Instance variables are part of the class . . .
Part of the object, surely?

I am sure you are right and there is confusion between fields and local variables.

Ah yes, thanks for pointing out the mistake.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
E Armitage wrote:
Campbell Ritchie wrote:.


I got it guys, you all were right. The error comes when you do not initialize the local variable.

" The local variable pw may not have been initialized "

Thanks!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Yes, local variable, not instance variable.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Jesper de Jong wrote:Yes, local variable, not instance variable.


Yes Sir
 
wood burning stoves
 
subject: Instance Variable