469,356 Members | 2,220 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

declaring a var inside a loop

Hello, I'm Jon.

I've recently picked up Java after using a "gimmick" programming language called GML ( Game Maker Language ). I've read a lot of tutorials and even a Java for Dummies *.pdf book. The basics are similar to what I'm accustomed to but there's still some confusion.

I'm currently playing around with how the static, public, protected etc things work and I stumbled upon a problem.

In the following copypasted code ( a tutorial I slightly modified ) the problem occurs inside the main(Strings args[]) thingy near the end.

Expand|Select|Wrap|Line Numbers
  1. public class ThisPointerExample {
  2.  
  3.   public static void main(String[] args) {
  4.     HumanBeing me = new HumanBeing("Brown");
  5.  
  6.  
  7.     while(true)
  8.     {
  9.         HumanBeing your = new HumanBeing("Blue"); /* *** noteworthy line *** */
  10.         break;
  11.     }
  12.  
  13.     System.out.println(me.isEqual(your)); /* *** problem is here with "your" It seems the variable "your" is not declared - that's what the error says anyways. I would have thought it woul be declared inside the while() loop *** */
  14.   }
  15. }
  16.  
  17. class HumanBeing {
  18.  
  19.   private String eyeColor;
  20.  
  21.   public HumanBeing(String color) {
  22.     this.eyeColor = color;
  23.  
  24.   }
  25.  
  26.   public String getEyeColor() {
  27.     return eyeColor;
  28.   }
  29.  
  30.   public boolean isEqual(HumanBeing your) {
  31.     return this.eyeColor.equals(your.getEyeColor());
  32.   }
  33. }
Could someone explain to me why the your variable isn't declared. And if it is declared, where is it? Since it seems the main() thingy doesn't recognize it.

Here are some of my thoughts:
1. A variable declared inside a loop will be discarded after the loop ends and the memory of this variable is freed because the variable is local to the loop and not the class.
2. However, adding a "this." inside the variable declaration doesn't fix this problem. I'm thinking this would tell java to store the variable with the class and not the loop - why doesn't this work? And what would be the proper way to declare a variable to a class inside a loop?

Thanks in advance.

- Jon
Mar 14 '09 #1
1 2640
JosAH
11,448 Expert 8TB
Indeed, that variable is declared in the body of that while loop and when that loop has finished your variable has gone out of scope as well and therefore doesn't exist anymore (you can't use it anymore). Also at every iteration of that loop a new variable is defined and any previously declared variable (from a previous pass through the loop if applicable) is gone.

kind regards,

Jos
Mar 14 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Oliver Corona | last post: by
3 posts views Thread by Grey Plastic | last post: by
18 posts views Thread by Nathan | last post: by
5 posts views Thread by Chris | last post: by
5 posts views Thread by Roy Smith | last post: by
8 posts views Thread by rendle | last post: by
6 posts views Thread by =?Utf-8?B?QUw=?= | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.