Results 1 to 11 of 11
- 04-07-2008, 10:37 AM #1
HELP: decleration and intialization in loops?
Hi,
i was using
Java Code:for ( i = 0; i < 10; i++ ) { MyClass myClassObject = new MyClass(); -- processing..... }
but later on i was told that i have to use;
Java Code:MyClass myClassObject = null; for ( i = 0; i < 10; i++ ) { myClassObject = new MyClass(); -- processing..... }
Both of the codes are producing the same result. Then why we should use the later one. Please let me know if you have know answer/reason. :eek:
Thanks
Rakesh
- 04-07-2008, 02:11 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you what mean this.
MyClass is one of your class in the project. So you try to use some functionality (fields/methods) in another class.Java Code:MyClass myClassObject = new MyClass();
myClassObject is the object reference to the class MyClass, don't get it in wrong way, it's not the object, it's a reference variable to the object. So
called the declaration.Java Code:MyClass myClassObject
Then what does new keyword do. Instantiate the object. Or in simple word create an object of class MyClass.
What is the MyClass(), actually it is the default constructor of the MyClass. Because there is no arguments (no-argument constructor also called default constructor in Java). Then here,
also called initialization of the object. After initialization memory is allocated with a reference to the object.Java Code:new MyClass();
So the different of two code segment is, in the first code you define a variable and initialize. In the second code you have done it in two places. And also in the second code, you no need to make the object reference to null.
I think it's help to you.
- 04-07-2008, 03:16 PM #3
:D say what
Thanks, I got everything :D
Now, please tell me which one we should prefer and why?
Thanks
Rakesh
- 04-08-2008, 03:27 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually it depends, on the scope the object you want to use. Normally it's better to define in the class level. Something like this,
So in that case you can use the same object reference in many methods.Java Code:class TestClass{ TestClassTwo objRef = new TestClassTwo(); void methodOne(){ // do the processing with objRef } void methodTwo(){ // do the processing with objRef } } class TestClassTwo{ }
Say that your scope is only within a method of a class. Then define it inside that method. But remember in that case you can't use the object reference outside of the method.
Java Code:class Test{ void method(){ TestTwo objRef = new TestTwo(); // do the processing using objRef only within this method. } } class TestTwo{ }
- 04-08-2008, 07:18 AM #5
Thanks, further clarification Looping
Yes, now i got that,
I am using the object withtin a single method, so no need to define it outside the method, and no need to make it global for the whole class.
But i was asking whether we should declare and intialize the object inside the Loop ( for / while ) , or should we declare it outside the loop and intialize later on in the loop. Or it doesn't matter at all?:eek:
Please see the first post. there i have clearly explained what i am asking now.:rolleyes:
Thanks Eranga for your replies, hope to see the next reply soon
Rakesh:)
- 04-08-2008, 07:22 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, tel me this quickly. Your for loop iterate 10 times. So what happened in following code.
Did you have any idea. I think you should, if you clear my early posts. :)Java Code:MyClass myClassObject = new MyClass();
- 04-08-2008, 07:33 AM #7
Test Season....
:p Okay.. then in the loop 10 times the object reference will be created and 10 times its constructor will be called.
while in other case, one time object reference is created and 10 times its constructor will be called. Hope i am correct.
:confused: But the point is whether there is any problem in former one, if i create the reference 10 times, becasue as soon as the loop will end 1st, 2nd 3rd .... times the respective reference will also be free, so there is no memory loss.
Let me know if i am wrong. Please. And if i am not wrong, then why we should not use former one.
Thanks
Rakesh
- 04-08-2008, 07:52 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually in the first code, you said there if you looping 10 times object reference created 10 times and initialized. So what happened to the memory. Memory allocated 10 times. Say you loop run 1000 times(in real world applications some loops are no limit, infinite loops) what happened to the memory.
But the story is different from what I said in the above few lines. Have you heard about Automatic Garbage Collection in Java. JVM automatically clear the memory, since the object is not longer used. What have done in you loop is, you define the same object reference. So what JVM do is overwrite the previous object reference and clear the initial memory and allocate the new memory. This is happened because of the loop. Since you define and initialized the object reference in the for loop, after the } sing of the loop, object reference is not used.
You are lucky with Java, because of the Automatic Garbage Collection. If you work with any other language you dead.
In my practice, both of your coding practice is 100% bad. I'm saying bad, not wrong. Few instance I prefer in my programming,
1.) If I want to use an object reference in the whole class or at least inside over one method, I declared and initialized in the class level. Used a single line to do this.
2.) If I want to use an object reference in a single method, declared in that method, may be the first code line of the method.
3.) Never declared/initialized inside loops.
4.) Even in some cases, to manage my code more robust manually clear some object reference.
Hope it is clear to you.
- 04-08-2008, 08:15 AM #9
Now that was really Cool.
First of all thank you very much for the support.
Here is my code.
Java Code:resultSet = preparedStatement.executeQuery(); // can be more than 100 while(resultSet.next()) { ReportResultBean reportBean = new ReportResultBean(); /* All the Setters of the bean will be here */ searchResultList.add( reportBean ); }
Now if you
declare and initialize reportBean before loop, and try to add this in List, then the result will not be what you are expecting.
declare reportBean before loop and initialize initialize the bean inside loop , and try to add this in List, or
declare and initialize reportBean inside loop , and try to add this in List, then
result will be fine.
Waiting for the expert comments,
Rakesh
- 04-08-2008, 08:19 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
searchResultList is one of your method right? If so why don't you ReportResultBean use there. I'm confusing with your code.
- 04-08-2008, 08:24 AM #11
searchResultList is a List
searchResultList is not any method, it is an ArrayListXML Code:searchResultList is one of your method right? If so why don't you ReportResultBean use there. I'm confusing with your code.
hope it is clear now.. waiting for replyJava Code:List searchResultList = new ArrayList();
Thanks
Also if searchResultList would be a method i don't know how do we use it like searchResultList.add ( xyz ); i guess it should be searchResultList().add(xyz), in that case :D
Similar Threads
-
Question about loops
By BHCluster in forum New To JavaReplies: 4Last Post: 04-16-2008, 05:40 PM -
[SOLVED] Need help with Loops...please!
By Zebra in forum New To JavaReplies: 5Last Post: 04-10-2008, 01:44 PM -
Loops (while do etc)
By manupr in forum New To JavaReplies: 1Last Post: 01-15-2008, 03:59 AM -
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08-06-2007, 04:51 PM -
Help me: loops in java
By silvia in forum New To JavaReplies: 3Last Post: 07-19-2007, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks