• 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

Generics, Collection Help: Sorty Object based on Name

 
Ranch Hand
Posts: 37
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic