aspose file tools*
The moose likes Beginning Java and the fly likes Generics, Collection Help: Sorty Object based on Name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics, Collection Help: Sorty Object based on Name " Watch "Generics, Collection Help: Sorty Object based on Name " New topic
Author

Generics, Collection Help: Sorty Object based on Name

Mayu Mayooresan
Ranch Hand

Joined: Mar 12, 2011
Posts: 37

I'm trying to sort objects based on the name. I don't know where Im going wrong. Code seems understandable and legal to me but keep on getting the error "The constructor SortMe.Man() is undefined" on top of New Man().



Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19002
    
  40

Mayu Mayooresan wrote: but keep on getting the error "The constructor SortMe.Man() is undefined" on top of New Man().


Basically, the compiler is complaining that you Man class doesn't have a constructor that takes no parameters. And if you take a quick look, your compiler is correct.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mayu Mayooresan
Ranch Hand

Joined: Mar 12, 2011
Posts: 37

Henry Wong wrote:
Mayu Mayooresan wrote: but keep on getting the error "The constructor SortMe.Man() is undefined" on top of New Man().


Basically, the compiler is complaining that you Man class doesn't have a constructor that takes no parameters. And if you take a quick look, your compiler is correct.

Henry


I made a empty constructor as below but now getting another error "No enclosing instance of type SortMe is accessible. Must qualify the allocation with an enclosing instance of type SortMe (e.g. x.new A() where x is an instance of SortMe)."



I removed the constructor so it'll use the default constructor. now getting this same error as above ( awwwwwwww

Abhay Agarwal
Ranch Hand

Joined: Feb 29, 2008
Posts: 1212
    
    1

error is coming because of wrong inner class instantiation

Correct Code would be



Note the instatnitation code of m1, m2 and m3 object. Class Man is innner class to SortMe class. Rest of your code is fine.
You can read below mentioned link for more details about instantiation code syntax
http://download.oracle.com/javase/tutorial/java/javaOO/nested.html



If you move class Man out of SortMe class (as shown in below code) then your previous code within main method of SortMe would work fine



Output of this code is
[javaapplication1.Man@19821f, javaapplication1.Man@addbf1, javaapplication1.Man@42e816]
[javaapplication1.Man@addbf1, javaapplication1.Man@42e816, javaapplication1.Man@19821f]


Hope I have cleared your doubt

Just a note that you can override toString() method of Man class to get better display of ArrayList contents (man objects)


Mayu Mayooresan
Ranch Hand

Joined: Mar 12, 2011
Posts: 37

Thanks a lot

Now I understand where I went wrong. I wrote the Man class inside the SortMe class and trying to code the rest thinking that Man Class is outside SortMe class

Yeah I tried to overide toString also and it worked like magic.

Thanks a lot again and again for your valuable time.

Cheers.
 
Don't get me started about those stupid light bulbs.
 
subject: Generics, Collection Help: Sorty Object based on Name