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]
# 1
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,
Martin@Stricenta at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 2
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.
new_rishia at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 3

> 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

kajbja at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 4

> 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.

Peetzorea at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 5

> 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

Martin@Stricenta at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 6

> > 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

kajbja at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 7
> 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).
jwentinga at 2007-7-9 22:47:10 > top of Java-index,Java Essentials,New To Java...
# 8
Right... slipped my mind!
Peetzorea at 2007-7-9 22:47:11 > top of Java-index,Java Essentials,New To Java...
# 9
> > That is a valid method declaration in an> interface.> > > A sloppy one thoughSloppy but valid :)
kajbja at 2007-7-9 22:47:11 > top of Java-index,Java Essentials,New To Java...
# 10
thanks a lot guys.... u all r real life savers!!!
new_rishia at 2007-7-9 22:47:11 > top of Java-index,Java Essentials,New To Java...