• 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

Trying to pass constructor parms into anonymous class

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Im fairly new to java. Have some experience but not much.

Im trying to use the javamail apis and Im trying to pass variables that Im passing into my constructor into the authentication class. This class is defined as an anonymous class. Ive tried every which way to do this. I know that I have to pass a final string into the class but it's failing to authenticate. I have tested by hardcoding the username and password and it works. But I cant seem to penetrate the anonymous firewall and pass it the variables.

Does anyone have a suggestion? Please be nice, Im attempting to learn and this is a huge struggle

I have an example. (one of many attempts)




--------------------------------------------------------------------------------------
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm no expert with anonymous classes, and this isn't any help for the problem you are trying to solve, but I don't think it is possible to pass parameters to an anonymous class constructor - you just have to use the no-arg version.
 
Ty Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for your reply. It is very much appreciated.

Is there a way that I can do a getUserName/getPassWord from within the anonymous class....

I should still have access to the this.variables, ?I think?

Im actually using the IBM language RPG to make JNI calls to java.

Problem is, I dont have access to the java console on the AS/400 to view my console display. Or at least I dont know how to view it ...
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot write a constructor for an anonymous class. You can however call one of its superclass’ constructors.If you create an anonymous class from an interface, which hasn’t got a constructor, you have to use the default constructor.
 
Ty Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You for your reply.

Im not actually(at least I dont think I am) trying to pass into an anonymous constructor. Im trying to make the variables available to the anonymous authentication method. I know it's not good practice but I made the variables public in the parent class and doing getters on them. But it's not working.

I thought that was the whole purpose of public declaration.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have an anonymous class, you don’t want to add fields. Simply use the fields from the superclass. If you want to add fields, you are probably better off declaring a new class, maybe as a private inner class. That can of course have its own constructor.
 
Ty Davis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You again or your reply.

Im not sure I understand what your saying. It's kinda over my head a bit.

Can you give me an example. Here is my code:


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear not to be using an anonymous class at all. You appear to be using a factory method. The method passes the requisite arguments to the constructor behind the scenes, and returns an object ready made.
That is something totally different from an anonymous class.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic