Results 1 to 12 of 12

Thread: Destroy object

  1. #1
    anjanesh is offline Member
    Join Date
    May 2007
    Location
    Mumbai, India
    Posts
    5
    Rep Power
    0

    Default Destroy object

    Hi

    How do I reuse the same object-name ?

    PHP Code:
    String s = new String("a-string");
    System.out.println(s);
    s = null;
    
    Integer s = new Integer(5);
    System.out.println(s);
    Java Code:
    Main.java:19: s is already defined in main(java.lang.String[])
                    String s = new String("b-string");
    I thought setting it to null would destroy it.

    Thanks
    Last edited by anjanesh; 12-13-2007 at 05:28 PM.

  2. #2
    rajiv_bang is offline Member
    Join Date
    Nov 2007
    Posts
    11
    Rep Power
    0

    Default

    Anjanesh,

    The code is trying to reuse the variable 's' and not an object (a reference actually). It is trying to declare a variable 's' again as an Integer when it has already been declared a few lines above as a String reference variable.

    Variable names in the same scope should be unique. You cannot have two variables with the same name in the same scope.

    However, if you want to use the same reference variable to point to a different object, you can do so.

    HTH

    Java Code:
    	String s = new String("a-string");
    	System.out.println(s);
    	s = null;
    
    	s = new String("b-string");
    	System.out.println(s);

  3. #3
    anjanesh is offline Member
    Join Date
    May 2007
    Location
    Mumbai, India
    Posts
    5
    Rep Power
    0

    Default

    Is there any way to destroy a variable so that it can be used to assign it to another type ?

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by anjanesh View Post
    Is there any way to destroy a variable so that it can be used to assign it to another type ?
    Rajiv will correct me if I'm wrong, but I'm pretty sure he answered this question.

    Variable names in the same scope should be unique. You cannot have two variables with the same name in the same scope.

    However, if you want to use the same reference variable to point to a different object, you can do so.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    I am trying to reuse a variable for the same obj. but i'm getting an error:
    Mnemonic a = new Mnemonic(4);
    a = null;
    Mnemonic a = new Mnemonic(5);

    ERROR:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    Duplicate local variable a

    this is in eclipse

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So then
    1) Don't have a second declaration of the variable, just use it.
    i.e., not
    Java Code:
    Mnemonic a = new Mnemonic(4);
    a = null;
    Mnemonic a = new Mnemonic(5);
    but instead
    Java Code:
    Mnemonic a = new Mnemonic(4);
    // a = null; // this isn't necessary
    a = new Mnemonic(5);
    2) Please don't resurrect long dead threads. If you have a question of your own, create a new thread here in the forum for this question.

  7. #7
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    oh, i see its an old problem, but one can avoid it if one set the right parenthesis

    Java Code:
    {
    String s = new String("a-string");
    System.out.println(s);
    s = null;
    }
    {
    Integer s = new Integer(5);
    System.out.println(s); 
    }
    yay !
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Quote Originally Posted by AndreB View Post
    oh, i see its an old problem, but one can avoid it if one set the right parenthesis

    Java Code:
    {
    String s = new String("a-string");
    System.out.println(s);
    s = null;
    }
    {
    Integer s = new Integer(5);
    System.out.println(s); 
    }
    yay !
    yup you can do that..i think the OP is confused with the concept of garbage collector

    Garbage

  9. #9
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    w8 so if you don't use { and }, you can't reuse a variable name for a different obj? how come?

  10. #10
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    "{" and "}" declares a code block like "begin" and "end". after "}" there is no variable "s" and therefore you can declare it again (Scope (programming) - Wikipedia, the free encyclopedia)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  11. #11
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    ohh alrigh. its like this basically:

    public class Test {

    public void test1() {
    int s = 5;
    .....
    }
    public void test2() {
    String s = "hi";
    .....
    }
    }
    Last edited by Lil_Aziz1; 01-02-2010 at 07:39 PM.

  12. #12
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    no rather
    Java Code:
    if (true) {
       ...
    }
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 0
    Last Post: 03-31-2008, 10:19 AM
  3. Applets (init, start, stop, destroy)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 10:57 AM
  4. Replies: 0
    Last Post: 09-28-2007, 12:51 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •