• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Local Var

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, is there a way to kill off a local var and declair it as somthing else in the method it is in?

Example
ArrayList <Lot> unsold = new ArrayList <Lot>();

and then kill it off and declair it as

String unsold = "Help"

Any help would be great
Mike
[ November 06, 2006: Message edited by: Mike Stevens ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't declare a variable twice in the same scope. You can assign it to something different though.

This sort of thing is asking for mistakes though. Do you have an example of why you need to do this? Or some existing code you are working on you could post?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you do this (I admit I haven't tried)?

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really need to use the same var name, you could just place them in different scope.

e.g.


But I would not recommend having 2 or more variables with the same names, even if they are not visible to each other.
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for checking it out. I'm very new to OOP and Java but have done other programming.

What I'm trying to do is have one return that has a value so

ArrayList <Lot> unsold = new ArrayList <Lot>();

if this

unsold = // Carries all the unsold lots in ArrayList

//if no lots
else
//rather than null
String unsold = "No unsold lots at this time";


So mabey what I want to do here is add a String to
ArrayList <Lot> unsold = new ArrayList <Lot>();

instead of trying to change the type

ArrayList <Lot> unsold

is this possible?

Here is my method
-----------------------------------------------------------

public ArrayList getUnsold()
{
ArrayList <Lot> unsold = new ArrayList <Lot>();
Iterator it = lots.iterator();
while(it.hasNext()) {
Lot lot = (Lot) it.next();
Bid highestBid = lot.getHighestBid();
String description = lot.getDescription();
int number = lot.getNumber();
if(highestBid == null) {
unsold.add(lot);
// this is here just so I know where and what is goin on
System.out.println("Lot number: " + number +
description + " has not been sold.");

}else{
unsold = ???;
}
}
return unsold;
}

------------------------------------------------------------------

This works if I get rid of the else. Or I put another return in it

The easy thing would be to use another return in the
}else{

return //some other local var

Thanks to all that has taken the time to take a look!
Mike
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
call the method
List unsold = getUnsoldList();
check the size of the returned list, print message
if(unsold.size() == 0) message "No unsold lots at this time";
[ November 06, 2006: Message edited by: Michael Dunn ]
 
Mike Stevens
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Michael, but I'm required to keep the method the same and also have to declare the

ArrayList <Lot> unsold = new ArrayList <Lot>();
part

The one return point is not required and just somthing I thought I would try to add a value in instead of having it return null

Thanks
Mike
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
possible different interpretations of 'call the method'

see if this is abit clearer

 
reply
    Bookmark Topic Watch Topic
  • New Topic