Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: An easy question

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default An easy question

    This is one of those that is hard to serach, but if you know what is happening it shouldn't be too bad to explain. I would even settle if you would just give me a term to research

    new javax.mail.Authenticator() {
    			protected PasswordAuthentication getPasswordAuthentication() {
    				return new PasswordAuthentication(username, password);
    			}
    		  });

    What is occuring here? Be very non-specefic (the snippet I gave is only a peice of code) I just want to know what function is being invoked above.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: An easy question

    You are creating an object of an anonymous class, which extends/implements javax.mail.Authenticator (depends on if javax.mail.Authenticator is a class or an interface).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    KAJLogic (December 1st, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,085
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: An easy question

    It will continue to be hard to search for with the unhepful thread title you've given this topic. Please give your topics better titles that describe the help needed or the Java subject to be discussed. For example, this topic concerns the javax.mail.Authenticator().

    What do the API page and/or Oracle tutorial tell you about the code you've posted?

  5. #4
    Member
    Join Date
    Sep 2013
    Posts
    102
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Default Re: An easy question

    Greg, I couldn't think of any better title (I will try harder next time). The idea though is javax.mail.Authenticator has nothing to do with my question. I was simply wondering what function was being applied. I could have done this to better illustrate my question:


    (A)
     
    Example ex = new path.to.use() {
                                 // do stuff 2
                               // what sort of things can I do here, and why would i use this?
                                                }


    I can disern from helloworld's post that class "use" has to be extended by my ex object. Will the above code actually apply a new construct with a whole new body?

    Let us say:
    (B)[CODE]
    public class use {
     
    public use(Object o) {
    // does stuff 1
     }
     
    }


    How does snippet A work with B? And, will stuff 1 & two be done? I will make a test class to try some of this out, but any backround on this sort of technique would be nice. It seems quite odd to me.

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: An easy question

    An anonymous class is just like a regular class, except it has no name. That means you can never refer to the type.

    new BaseType(base_parameters){
        // pretty much everything you can put into a normal class
        // except anonymous classes have no explicit constructors
        public void method1()
        {}
        private int field1;
     
    };

    This is similar to (but NOT exactly the same) wrapping the class definition with an instantiation of an object of that type:
    class AnonType extends BaseType // note: could be an interface, in which case use implements
    {
        public void method1() {}
        private int field1;
    }
     
    // ...
    new AnonType();

    There are a few things that anonymous classes can do that normal classes can't (and vise-versa), see: Anonymous Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  7. The Following User Says Thank You to helloworld922 For This Useful Post:

    KAJLogic (December 1st, 2013)

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,085
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: An easy question

    I understand and agree that it's hard to pick an appropriate thread title when you have no idea what the topic of your question is called. However, my experience is that threads titled "Easy . . ." never are.

    You found the most obscure example of an anonymous inner class I've ever seen. A reference for the example or a link to it might have been helpful. If you're actually studying the code you posted or code of that complexity, you may be a bit ahead of yourself. It's okay to stretch one's reach for knowledge, but it's important to already have the appropriate foundation on which to add the new info or to backfill those weak areas as they are discovered. Now that you know about anonymous inner classes, you should write several of your own examples using the links provided by helloworld922.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    KAJLogic (December 1st, 2013)

Similar Threads

  1. [SOLVED] Absolute Beginner Here, should be an easy question for anyone really.
    By Harrald in forum What's Wrong With My Code?
    Replies: 27
    Last Post: July 31st, 2012, 09:40 PM
  2. How do I get this to work? Easy question, I'm just hopeless at it!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 1st, 2012, 01:31 PM
  3. Exception in JUnit test (easy question)
    By opium in forum Exceptions
    Replies: 1
    Last Post: February 14th, 2012, 08:09 AM
  4. Easy array question
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 08:44 PM
  5. Quick easy Java question.
    By DHG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2011, 02:59 PM