Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics, Collection Help: Sorty Object based on Name

 
Mayu Mayooresan
Ranch Hand
Posts: 37
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20880
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mayu Mayooresan
Ranch Hand
Posts: 37
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic