Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-13-2007, 06:18 PM
Member
 
Join Date: May 2007
Location: Mumbai, India
Posts: 5
anjanesh is on a distinguished road
Destroy object
Hi

How do I reuse the same object-name ?

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

Integer s = new Integer(5);
System.out.println(s); 
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 06:28 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-15-2007, 04:06 AM
Member
 
Join Date: Nov 2007
Posts: 11
rajiv_bang is on a distinguished road
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

Code:
String s = new String("a-string"); System.out.println(s); s = null; s = new String("b-string"); System.out.println(s);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-15-2007, 05:28 AM
Member
 
Join Date: May 2007
Location: Mumbai, India
Posts: 5
anjanesh is on a distinguished road
Is there any way to destroy a variable so that it can be used to assign it to another type ?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-31-2007, 05:19 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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.

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default initialization & destroy methods in Spring Framework Java Tip Java Tips 0 03-31-2008 11:19 AM
Applets (init, start, stop, destroy) Java Tip Java Tips 0 12-12-2007 11:57 AM
Default initialization & destroy methods in Spring Framework JavaBean Java Tips 0 09-28-2007 01:51 PM
Creating object of Type Object class venkatv New To Java 3 07-17-2007 04:33 PM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 04:19 PM


All times are GMT +3. The time now is 09:56 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org