Results 1 to 11 of 11
  1. #1
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default 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

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Do you what mean this.

    Java Code:
    MyClass myClassObject = new MyClass();
    MyClass is one of your class in the project. So you try to use some functionality (fields/methods) in another class.

    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

    Java Code:
    MyClass myClassObject
    called the declaration.

    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,

    Java Code:
    new MyClass();
    also called initialization of the object. After initialization memory is allocated with a reference to the object.

    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.

  3. #3
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default :D say what

    Thanks, I got everything :D

    Now, please tell me which one we should prefer and why?


    Thanks
    Rakesh

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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,

    Java Code:
    class TestClass{
    
      TestClassTwo objRef = new TestClassTwo();
    
      void methodOne(){
         // do the processing with objRef
      }
      void methodTwo(){
        // do the processing with objRef
      }
    }
    
    class TestClassTwo{
    }
    So in that case you can use the same object reference in many methods.

    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{
    }

  5. #5
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default 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:)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, tel me this quickly. Your for loop iterate 10 times. So what happened in following code.

    Java Code:
    MyClass myClassObject = new MyClass();
    Did you have any idea. I think you should, if you clear my early posts. :)

  7. #7
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Lightbulb 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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  9. #9
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default 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

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    searchResultList is one of your method right? If so why don't you ReportResultBean use there. I'm confusing with your code.

  11. #11
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default searchResultList is a List

    XML Code:
    searchResultList is one of your method right? If so why don't you ReportResultBean use there. I'm confusing with your code.
    searchResultList is not any method, it is an ArrayList
    Java Code:
    List searchResultList = new ArrayList();
    hope it is clear now.. waiting for reply


    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

  1. Question about loops
    By BHCluster in forum New To Java
    Replies: 4
    Last Post: 04-16-2008, 06:40 PM
  2. [SOLVED] Need help with Loops...please!
    By Zebra in forum New To Java
    Replies: 5
    Last Post: 04-10-2008, 02:44 PM
  3. Loops (while do etc)
    By manupr in forum New To Java
    Replies: 1
    Last Post: 01-15-2008, 04:59 AM
  4. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 05:51 PM
  5. Help me: loops in java
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-19-2007, 07:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •