This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Confused with nested class declaration from constructors... I don't know

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand , as to how these things work , as to how can one open a class from a constructor being passed as a parameter to some other function... Phew! Please have a look
help-2.PNG
[Thumbnail for help-2.PNG]
 
Ranch Foreman
Posts: 82
1
VI Editor Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand much what you mean by 'open a class', but the concept you're referring to, is Anonymous class. It's often used as in your case for Event Listeners in GUI applications. However, starting from Java 8 you can also use cleaner approach with Functional Interfaces and lambda expressions (you can find the reference for lambda expressions in that article also).
 
Marshal
Posts: 68112
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid I found your question hard to understand. You don't open classes; you instantiate them. I don't understand what you mean about passing a constructor to another function. Maybe by function you mean method?
What follows was good code sixteen years ago when we used Java1.4:-Maybe that is what you were asking about.
The new Integer(123) part is an instance creation expression. There are at least four things you can do with an instance creation expression:-
  • 1: Ignore it. A bit if a waste of time.
  • 2: Use it and let it disappear. That would work if the constructor contains enough information to do something. Usually probably a bad idea.
  • 3: Assign it to an identifier. That is what you will be most familiar with.
  • 4: Use it once and once only. You might pass the expression as an argument to a constructor or method, or after return or throw or similar. That is what I showed above.
  •  
    Campbell Ritchie
    Marshal
    Posts: 68112
    258
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please don't edit posts like that. It makes me look as if I hadn't read what you were asking about. Which I hadn't because it wasn't there at the time, and the screenshot doesn't appear on the reply window. Screenshots are usually bad things anyway.
     
    Campbell Ritchie
    Marshal
    Posts: 68112
    258
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Miroslav Lehotsky wrote:. . . Functional Interfaces and lambda expressions . . .

    Unfortunately, WindowListener isn't a functional interface, so that approach won't work here.
     
    Gourav Das
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:I am afraid I found your question hard to understand. You don't open classes; you instantiate them. I don't understand what you mean about passing a constructor to another function. Maybe by function you mean method?
    What follows was good code sixteen years ago when we used Java1.4:-Maybe that is what you were asking about.
    The new Integer(123) part is an instance creation expression. There are at least four things you can do with an instance creation expression:-

  • 1: Ignore it. A bit if a waste of time.
  • 2: Use it and let it disappear. That would work if the constructor contains enough information to do something. Usually probably a bad idea.
  • 3: Assign it to an identifier. That is what you will be most familiar with.
  • 4: Use it once and once only. You might pass the expression as an argument to a constructor or method, or after return or throw or similar. That is what I showed above.



  • Clearly , i get your point , but i have a query!

    How can i access the specific parent class method which has been overriden by an anonymous class , like this example






    But how can i change the output to "Hello There" ?
     
    Campbell Ritchie
    Marshal
    Posts: 68112
    258
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Gourav Das wrote:. . .How can i access the specific parent class method which has been overriden . . .

    Say superclass (or base class, like in C#) not parent.

    You can't. Full stop. Because of polymorphism you always get the “nearest” overriding. With static methods, you can access the superclass method with a cast, because you can get confused about which method you are calling. Always call static methods on the class name.
     
    Campbell Ritchie
    Marshal
    Posts: 68112
    258
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can write super.someMethod(); in any instance method.
     
    It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!