473,322 Members | 1,690 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,322 software developers and data experts.

creating object in for loop

17
Expand|Select|Wrap|Line Numbers
  1. public class mainclass1 {
  2.     public static void main(String[] args) {
  3.           for(int i= 0;i<1;i++) 
  4.                     a obj  = new a();
  5.          }
  6. }
  7.  
  8. class a{
  9. }
  10.  
In the above program if you try to create an object with in "for" loop then you will get an error stating "not a statement "
ex:
Expand|Select|Wrap|Line Numbers
  1. for(int i= 0;i<1;i++) 
  2.     a obj  = new a();

but if you try to create an object with brackets then it will create without an error
ex:
Expand|Select|Wrap|Line Numbers
  1. for(int i= 0;i<1;i++) {
  2.     a obj  = new a(); 
  3. }
why is it so , and whats the problem , Please explain it.

Thanks in advance.
Jan 30 '08 #1
2 1820
BigDaddyLH
1,216 Expert 1GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Jan 30 '08 #2
BigDaddyLH
1,216 Expert 1GB
That's just how the syntax is defined. You can unravel it here: http://java.sun.com/docs/books/jls/t...ntax.html#18.1

Basically, the body of a for loop can be many things, including a block {...}, but the body can't be a LocalVariableDeclarationStatement. What's the lesson? None, really. Most Java developers automatically put braces around their loop bodies -- problem, solved.
Jan 30 '08 #3

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

Similar topics

6
by: NotGiven | last post by:
I want to learn moer of what I saw in a recent example. They create a page that created new fields/element. It's not like they were hidden and they displayed them, they were not there, then the...
6
by: Mike MacSween | last post by:
like this: Dim rst As Recordset Set rst = CurrentDb.OpenRecordset(strSql) rst.MoveFirst Do While Not rst.EOF Set oMailItem = oOutlook.CreateItem(olMailItem) With oMailItem .To =...
2
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say,...
9
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks ...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
2
by: RSH | last post by:
How do I go about creating an object name in a loop? I have a class named objSample Dim j As Integer For j = 1 To 10 Dim inpFile As System.Web.UI.HtmlControls.HtmlInputFile =
5
by: fireball | last post by:
please help newbie I need to create a lot of objects the same type (let's say: schemas) I wish to use paramerized block in loop to do so. - how to put names of my objects to such control-flow? ...
6
by: RSH | last post by:
Hi, i have a situation where I need to dynamically create objects in a loop. My question surrounds intantiation naming in such a scenerio. Below is a snippet that is basically hardcoding each...
4
by: =?Utf-8?B?VG9kZCBKYXNwZXJz?= | last post by:
Hey guys, Is there ANY way to accomplish this: (see below)? Basically, I want to have a loop (a < 3 is just for testing purposes, it will be an underermined amount). In this loop, I want to be...
2
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.