aspose file tools*
The moose likes Beginning Java and the fly likes Trying to pass constructor parms into anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to pass constructor parms into anonymous class" Watch "Trying to pass constructor parms into anonymous class" New topic
Author

Trying to pass constructor parms into anonymous class

Ty Davis
Greenhorn

Joined: Mar 01, 2013
Posts: 6
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)




--------------------------------------------------------------------------------------
Douglas Mortimer
Greenhorn

Joined: Mar 30, 2000
Posts: 7
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

Joined: Mar 01, 2013
Posts: 6
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 ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
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

Joined: Mar 01, 2013
Posts: 6
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
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
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

Joined: Mar 01, 2013
Posts: 6
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
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to pass constructor parms into anonymous class