Page 53 of 55 FirstFirst ... 3435152535455 LastLast
Results 1,041 to 1,060 of 1093

Thread: Quiz Time

  1. #1041
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Ramanjaneyulu View Post
    We know that if one thread is having class lock on an object,no other threads cann't access any other methods of that object.

    Can any one give example to demonstrate the above point.
    The idea of this thread is totally different than other. It's more on discussing simple quizzes among the members. If you have a question please start a new thread in the appropriate sub-forum, since your question is not related to the original question of a thread.

  2. #1042
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Ramanjaneyulu View Post
    We know that if one thread is having class lock on an object,no other threads cann't access any other methods of that object.

    Can any one give example to demonstrate the above point.
    It it not true: if one thread synchronizes on an object while another one doesn't the other thread can still ruin everything. In order to establish proper locking the different threads have to play the game by the rules.

    kind regards,

    Jos

  3. #1043
    Cbani is offline Member
    Join Date
    Jan 2010
    Posts
    90
    Rep Power
    0

    Default

    Here goes one more

    Java Code:
    public class StaticTest {	
    
    	public static int x = 0;
    	
    	public static StaticTest staticTest = new StaticTest();
    	
    	static {
    		System.out.println("static..."+ ++x);
    	}	
    	
    	public StaticTest() {
    		System.out.println("constructor"+ ++x);
    	}
    }
    Now from some other class I will do something like

    Java Code:
    public class Test {	
    
    	public static void main(String args[]) {
    		StaticTest staticTest = new StaticTest();
    	}
    }
    Now, what would be the sequence of S.O.P.(s)?
    Let's not use our editors before answering.

  4. #1044
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    After a long time. :D

    In this order,

    Java Code:
    System.out.println("constructor"+ ++x);
    System.out.println("static..."+ ++x);
    System.out.println("constructor"+ ++x);

  5. #1045
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    It will print 'constructor' then 'static' then 'constructor' with x 1,2,3 respectively

    By the way hats off to you guys its an awesome thread !!!!!! Thnks a lot :)
    ____________________________
    Give me beans .........
    Last edited by Vinod Mukundan; 11-24-2010 at 12:59 PM.

  6. #1046
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  7. #1047
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Here,s another one; it's from the book "Java puzzlers" by Joshua Bloch and Neal Gafter. The Following code doesn't compile:

    Java Code:
    public class Outer {
       class Inner1 extends Outer { }
       class Inner2 extends Inner1 { }
    }
    Questions: why doesn't it compile and how can you alter the code so that it compiles?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #1048
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Supper class of Inner2 class, that is Inner1 class, itself is an inner class. So the default constructor of Inner2 class is illegal.

    Am I correct?

    If I'm correct I wonder how to fix this.

  9. #1049
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Here,s another one; it's from the book "Java puzzlers" by Joshua Bloch and Neal Gafter.
    Can you share it with me? :rolleyes:

  10. #1050
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    What about defining inner1 class as static or in that case even private :p srry I tried it in IDE ...jus thought might share



    warm regards
    Vinod M
    ____________________________
    Give me beans .........
    Last edited by Vinod Mukundan; 11-25-2010 at 11:06 AM.

  11. #1051
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    Supper class of Inner2 class, that is Inner1 class, itself is an inner class. So the default constructor of Inner2 class is illegal.

    Am I correct?
    Why should that matter? Both Inner1 and Inner2 are inner classes and Inner2 extends Inner1; that shouldn't be a problem ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #1052
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Vinod Mukundan View Post
    What about defining inner1 class as static or in that case even private :p srry I tried it in IDE ...jus thought might share
    No, I'm looking for a fix where none of the inner classes are made static.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  13. #1053
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    No, I'm looking for a fix where none of the inner classes are made static.

    kind regards,

    Jos ;-)

    Ohh sure that was jus a suggestion .... by the way I got the solution . I read the entire puzzle itself ... was too anxious :) and could not control .... Well my reasoning was close ... Dont want to mess the thrill here ...:p Let the ball keep rolling.

    warm regards
    Vinod M
    ____________________________
    Give me beans .........

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

    Default

    Stick Inner2 inside Inner1.

  15. #1055
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Stick Inner2 inside Inner1.
    That doesn't make the compilation error go away. I want Inner1 and Inner2 two (non nested) inner classes of class Outer; how to fix?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #1056
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    Why should that matter? Both Inner1 and Inner2 are inner classes and Inner2 extends Inner1; that shouldn't be a problem ...

    kind regards,

    Jos
    Because, default constructor requires an enclosing instance to be supplied.

  17. #1057
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Can we avoid it by calling super type (Inner2) constructor here?

    Java Code:
    Inner2() { Outer.this.super(); }

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

    Default

    Quote Originally Posted by JosAH View Post
    That doesn't make the compilation error go away. I want Inner1 and Inner2 two (non nested) inner classes of class Outer; how to fix?

    kind regards,

    Jos
    I didn't expect it would, to be honest...:)

  19. #1059
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    Because, default constructor requires an enclosing instance to be supplied.
    You have been reading the compiler's diagnostic message but your explanation is not the reason why it doesn't compile. Try it with two non inner classes and all will be fine.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #1060
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Eranga View Post
    Can we avoid it by calling super type (Inner2) constructor here?

    Java Code:
    Inner2() { Outer.this.super(); }
    Why?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Hello, first time here.
    By ludragon in forum Introductions
    Replies: 2
    Last Post: 01-03-2008, 06:03 AM
  2. Help pls with a quiz
    By saytri in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 07:09 AM
  3. Time method
    By carderne in forum New To Java
    Replies: 5
    Last Post: 11-05-2007, 10:34 AM
  4. DataObject with the time given by me
    By garinapavan in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 07:33 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
  •