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: Function in Function?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Function in Function?

    Hi! I'm a newbie in Java programming, and I'm learning from some open-source projects now. I've just came across a question: what does this block mean? I got surprised by the method defined with another method, which is rarely seen in C++. Can someone explain a little for me? Thanks a lot!

    P.S.: Here we see a 'new' operation that creates an Authenticator object, but I don't understand what the block means that follows the new operation. It seems to have defined a 'getPasswordAuthentication()' method there, but why it is defined here, not in the class?

    Authenticator.setDefault
    			(
    				new Authenticator()
    				{
    					protected PasswordAuthentication getPasswordAuthentication()
    					{
    						PasswordAuthentication passwordAuthentication = null;
     
    						if(this.getRequestorType() == Authenticator.RequestorType.PROXY)
    						{
    							if(this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTP") == true)
    							{
    								passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTP_PROXY_SERVER_USERNAME, APJP.APJP_HTTP_PROXY_SERVER_PASSWORD.toCharArray());
    							}
    							else
    							{
    								if(this.getRequestingURL().getProtocol().equalsIgnoreCase("HTTPS") == true)
    								{
    									passwordAuthentication = new PasswordAuthentication(APJP.APJP_HTTPS_PROXY_SERVER_USERNAME, APJP.APJP_HTTPS_PROXY_SERVER_PASSWORD.toCharArray());
    								}
    							}
    						}
     
    						return passwordAuthentication;
    					}
    				}
    			);


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function in Function?

    Oh, and the resource file can be found at
    https://apjp.googlecode.com/svn/trun...APJP/Main.java

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Function in Function?

    the block means that follows the new operation.
    That is an anonymous class. Google define anonymous class in java
    for descriptions:
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function in Function?

    Quote Originally Posted by Norm View Post
    That is an anonymous class. Google define anonymous class in java
    for descriptions:
    Thanks, Norm, I'm gonna Google the term 'anonymous class' and find out what that is for.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Function in Function?

    Now I got it. It's not creating an instance of Authenticator class, but extending it. As said by Norm, an 'anonymous class' is used here, and the function defined within the anonymous class is overriding the one from Authenticator.
    This is really different from C++. I'm finding Java more interesting now.

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Function in Function?

    Java refers to them as methods, not functions

Similar Threads

  1. Simple yes/no function...
    By JGstewart95 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 11th, 2011, 09:32 AM
  2. need help with a function...
    By fallout87 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 2nd, 2011, 05:42 AM
  3. function timeDiff
    By bobby321 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 25th, 2011, 02:23 PM
  4. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 11:29 AM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM