Results 1 to 6 of 6

Thread: wierd question

  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default wierd question

    Java Code:
     
    
    public class Questions{
    	
    	public interface Printer{
    		public void print (int idx);
    	}
    	
    	public static void print (Printer p){
    		for (int i = 0; i <4; i++){
    			p.print(i);
    		}
    	}
    	 public static void main(String[] args){
    		 print (new Printer()  {
    			 public void print(int idx){
    				 System.out.println(idx);
    			 }
    		 });
    	 }
    	
    }

    I'm having trouble understanding what this does. Could someone talk me through what is going on, it's confusing me a lot.

    Thanks very much

  2. #2
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: wierd question

    anyone?

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: wierd question

    Well it looks like the Questions class contains an interface named Printer (similar to a class containing an inner class). That interface only has one function that is implemented by the object created in main. That object is passed to the Question class print function, which uses the object to print the number 0 thru 3.
    Last edited by shall; 05-16-2012 at 02:52 AM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: wierd question

    Forum Rules
    Especially the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    homesting is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: wierd question

    If change this code to follow, then better to understand.
    Java Code:
    public class Questions{ 
         
         public interface Printer{ 
         public void print (int idx); 
         } 
         
         public static void print (Printer p){ 
         for (int i = 0; i <4; i++){ 
         p.print(i); 
         } 
         } 
         public static void main(String[] args){ 
         print (new CustomPrinter); 
         } 
         class CustomPrinter implements Printer{
             public void print(int idx){ 
         System.out.println(idx); 
         } 
         }
         
        }
    Last edited by homesting; 05-16-2012 at 07:45 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: wierd question

    Well, apart from the formatting nightmare and the fact it doesn't compile...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. JPanel on JFrame wierd problem
    By IndiDev in forum AWT / Swing
    Replies: 3
    Last Post: 02-06-2012, 08:30 PM
  2. wierd NetBeans problem.
    By edi.gotieb in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2010, 02:31 PM
  3. Wierd problem with abstract and extends
    By g123456 in forum New To Java
    Replies: 6
    Last Post: 05-14-2010, 07:25 PM
  4. Wierd delay in DefaultListModel.addElement()
    By DaedalusAlpha in forum AWT / Swing
    Replies: 4
    Last Post: 04-19-2010, 08:51 AM
  5. wierd problem
    By f_the_cook in forum Advanced Java
    Replies: 4
    Last Post: 10-09-2008, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •