473,375 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

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 {
  3.   public static void main(String[] args) {
  4.     HumanBeing me = new HumanBeing("Brown");
  7.     while(true)
  8.     {
  9.         HumanBeing your = new HumanBeing("Blue"); /* *** noteworthy line *** */
  10.         break;
  11.     }
  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. }
  17. class HumanBeing {
  19.   private String eyeColor;
  21.   public HumanBeing(String color) {
  22.     this.eyeColor = color;
  24.   }
  26.   public String getEyeColor() {
  27.     return eyeColor;
  28.   }
  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 3019
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,

Mar 14 '09 #2

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

Similar topics

by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is allocating memory for a new variable more...
by: Grey Plastic | last post by:
I'm looking for a way to declare variables inside for statements (or perhaps some other statement) and have the following statement execute exactly once. For example, for(Type var=blah; 1; ) ...
by: Nathan | last post by:
If you're wondering why I post so many questions, it's because I want to make an entry in the Guinness Book of World Records. But because I post so many, I try to make them simple. Here is (I...
by: Chris | last post by:
Is there any difference (memory/speed wise) between these two snipits. I always like to write in snipit 2 style, but was just curious. I'm scared I'd declare a thousand pointers when I don't need...
by: Roy Smith | last post by:
The following code appears to be illegal: while ((int c = getchar()) != EOF) { putchar (c); } I tried it on two different compilers (Sun workshop and gcc), and both give some variation on...
by: rendle | last post by:
I have a MSIL/performance question: Is there any difference between declaring a variable once and assigning to it multiple times, and declaring and assigning multiple times? For example: //...
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i < 10; i++) {
by: Dom | last post by:
I should know this, but I don't. In the following, is an integer being created with each iteration? for (...) { int n = SomeFunction (); } Or should I do the following, even if I don't...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.