Results 1 to 12 of 12
Thread: Destroy object
- 12-13-2007, 05:18 PM #1
Member
- Join Date
- May 2007
- Location
- Mumbai, India
- Posts
- 5
- Rep Power
- 0
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);I thought setting it to null would destroy it.Java Code:Main.java:19: s is already defined in main(java.lang.String[]) String s = new String("b-string");
ThanksLast edited by anjanesh; 12-13-2007 at 05:28 PM.
- 12-15-2007, 03:06 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 11
- Rep Power
- 0
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);
- 12-15-2007, 04:28 AM #3
Member
- Join Date
- May 2007
- Location
- Mumbai, India
- Posts
- 5
- Rep Power
- 0
Is there any way to destroy a variable so that it can be used to assign it to another type ?
- 12-31-2007, 04:19 AM #4
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.
- 01-01-2010, 05:24 AM #5
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
-
So then
1) Don't have a second declaration of the variable, just use it.
i.e., not
but insteadJava Code:Mnemonic a = new Mnemonic(4); a = null; Mnemonic 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.Java Code:Mnemonic a = new Mnemonic(4); // a = null; // this isn't necessary a = new Mnemonic(5);
- 01-02-2010, 11:39 AM #7
oh, i see its an old problem, but one can avoid it if one set the right parenthesis
yay !Java Code:{ String s = new String("a-string"); System.out.println(s); s = null; } { Integer s = new Integer(5); System.out.println(s); }"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 01-02-2010, 02:18 PM #8
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
yup you can do that..i think the OP is confused with the concept of garbage collector
Garbage
- 01-02-2010, 04:25 PM #9
w8 so if you don't use { and }, you can't reuse a variable name for a different obj? how come?
- 01-02-2010, 05:54 PM #10
"{" 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 ?"
- 01-02-2010, 06:46 PM #11
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.
- 01-02-2010, 07:25 PM #12
no rather
Java Code:if (true) { ... }"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
Similar Threads
-
Operator < cannot be applied to java.lang.Object, Object
By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 02:12 AM -
Default initialization & destroy methods in Spring Framework
By Java Tip in forum Java TipReplies: 0Last Post: 03-31-2008, 10:19 AM -
Applets (init, start, stop, destroy)
By Java Tip in forum Java TipReplies: 0Last Post: 12-12-2007, 10:57 AM -
Default initialization & destroy methods in Spring Framework
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:51 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks