• Post Reply Bookmark Topic Watch Topic
  • New Topic

Local Variables  RSS feed

 
Tuesday Ganal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm new in this forum and I was wondering if anybody can help me with my question about local variables. I was reading the SCJA certification guide by Cameron Mckenzie and I have read that "local variables" should be initialized. I was taking a short course which is Java Complete Curriculum and I did some exercises in which I don't need to initialize local variables. The program was compiled successfully and I was able to run it too. I'm preparing to take my SCJA exam and I want to know if what I have read about local variables is correct.

Thanks
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tess Bueno wrote:Hi All,

I'm new in this forum and I was wondering if anybody can help me with my question about local variables. I was reading the SCJA certification guide by Cameron Mckenzie and I have read that "local variables" should be initialized. I was taking a short course which is Java Complete Curriculum and I did some exercises in which I don't need to initialize local variables. The program was compiled successfully and I was able to run it too. I'm preparing to take my SCJA exam and I want to know if what I have read about local variables is correct.

Thanks


Hi Tess, Welcome to Java Ranch!

What you read about Local variables is correct- They have to be initialised. What you would have seen in you Java Complete Curriculum would be the Instance variables- you need not initialise the instance variables as they will be assigned their default values.

Local variables- the ones declared and used within the scope of the method/block
Instance Variables- those variables which are declared within the class (part of the class declaration).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tess Bueno, Welcome to JR. use black letters, they will be more readable. And, Could you post that code snaps?
 
swaraj gupta
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome Tess Bueno....
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:. . . use black letters, they will be more readable. . . .
Agree. That block of red is hard on the eyes; I shall have to get rid of it.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Abimaran Kugathasan wrote:. . . use black letters, they will be more readable. . . .
Agree. That block of red is hard on the eyes; I shall have to get rid of it.

Thanks Campbell Ritchie for considering it.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome , and thank you for noticing it.
 
Tuesday Ganal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, guys. I hope that I'll get many tips from this site. I'll be taking my SCJA exam few months from now.

Thanks, Campbell Ritchie for your reply. I'm new to Java. I just finished the 4 modules in preparation for my SCJA exam. Those local variables that I'm referring to are declared inside the methods, and I didn't receive any errors while compiling it. Maybe the reason why It was successfully compiled is, I used the javac command to compile it.

Sorry for the red colored text.
 
Kurt Van Etten
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tess,

There are few circumstances under which you might not need to initialize a local variable. First, if you never actually use the variable, you'll be fine! Second, if you're using an array then you need to instantiate the array itself, but you don't need to initialize the array elements. If neither of those apply to your situation, then I'd be curious to take a look at a sample of your code and see what else is going on.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kurt Van Etten wrote: . . . if you never actually use the variable . . .
In which case, why have the variable in the first place?
 
Tuesday Ganal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Kurt! I think you're right. What I did was, I only used the local variable once. I might have to try use the variable twice.


Kurt Van Etten wrote:Hi Tess,

There are few circumstances under which you might not need to initialize a local variable. First, if you never actually use the variable, you'll be fine! Second, if you're using an array then you need to instantiate the array itself, but you don't need to initialize the array elements. If neither of those apply to your situation, then I'd be curious to take a look at a sample of your code and see what else is going on.
 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tess Bueno wrote:Thanks, Kurt! I think you're right. What I did was, I only used the local variable once. I might have to try use the variable twice.


Kurt Van Etten wrote:Hi Tess,

There are few circumstances under which you might not need to initialize a local variable. First, if you never actually use the variable, you'll be fine! Second, if you're using an array then you need to instantiate the array itself, but you don't need to initialize the array elements. If neither of those apply to your situation, then I'd be curious to take a look at a sample of your code and see what else is going on.


If you don't initialize a local variable you cannot 'use' it at all generally. You can 'declare' a local variable without initializing it but as soon as you try to 'read' it you will get a compiler error. As long as you only declare it and don't initialize it but then you never actually read it, no problem...except that you've got a pointless variable floating around in your code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!