code walkthrough
i am a new student of java and i have been given a task to understand a code and make few changes to the code. while doing the code walkthrough i came across two statements that i cannot understand. can anyone tell me what exactly they mean the first statement is
public Set<XYZInterface> getXYZCollection();
and the second one is
Set<XYZInterface> XYZCollection = new HashSet<XYZInterface>();
I will be really thankful to you guys if you could help me understand it.
Thanks!
[532 byte] By [
new_rishia] at [2007-11-26 19:54:38]

It is very hard to answer your questions if you don't explain WHAT in the above rows you don't understand. Please try and do so,
to be very frank with u sir, i understand nothing in those declarations. it is fine if you dont give me the explanation, if you could suggest me a link that has any material to read which will help me understand this, it will be really nice of you.
> public Set<XYZInterface> getXYZCollection();
That declares a method named getXYZCollection. The method returns a Set which contains instances of XYZInterface.
> Set<XYZInterface> XYZCollection = new
> HashSet<XYZInterface>();
I think you can guess what this does if you take a look at what I wrote above.
http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html
http://java.sun.com/docs/books/tutorial/java/javaOO/variables.html
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Kaj
> Set<XYZInterface> XYZCollection = new HashSet<XYZInterface>();
This is just a declaration of a collection. It declares a variable called XYZCollection of type Set<XYZInterface>. Set is an interface. The object it references to is of type HashSet<XYZInterface>, which implements the Set interface. The part between < and > defines that the set can only contain items of type XYZInterface (so instances of classes that implement this interface).
> public Set<XYZInterface> getXYZCollection();
My guess is that you mistyped something here. Either it is a method definition in an abstract class and you forgot to add the abstract-modifier, or it is a declaration of a variable of type Set<XYZInterface> that gets initialised by calling getXYZCollection(). In the latter case you forgot to type your variable name and assignment operator.
> i am a new student of java and i have been given a
> task to understand a code and make few changes to the
> code. while doing the code walkthrough i came across
> two statements that i cannot understand. can anyone
> tell me what exactly they mean the first statement is
>
>
> public Set<XYZInterface> getXYZCollection();
>
The method has public access and returns a Set of XYZInterfaces.
> and the second one is
>
> Set<XYZInterface> XYZCollection = new
> HashSet<XYZInterface>();
A Set of XYZInterfaces is instansiated as a HashSet of ZYZInterfaces.
You need to read:
http://java.sun.com/docs/books/tutorial/collections/index.html
and:
http://java.sun.com/docs/books/tutorial/java/generics/index.html
> > public Set<XYZInterface> getXYZCollection();
> My guess is that you mistyped something here. Either
> it is a method definition in an abstract class and
> you forgot to add the abstract-modifier, or it is a
> declaration of a variable of type Set<XYZInterface>
> that gets initialised by calling getXYZCollection().
> In the latter case you forgot to type your variable
> name and assignment operator.
That is a valid method declaration in an interface.
Kaj
> That is a valid method declaration in an interface.> A sloppy one though, the "public" declaration is superfluous and really shouldn't be there (as it is implicit).
Right... slipped my mind!
> > That is a valid method declaration in an> interface.> > > A sloppy one thoughSloppy but valid :)
thanks a lot guys.... u all r real life savers!!!