0

I have written this code and this generate the error like Illegal start of expression on the line of run() method ,please solve my problem.

     class Reentrant 
        {
            public synchronized void m() 
            {
                 n();  
                    System.out.println("this is m() method");  
                }  


            public synchronized void n() 
            {  
                    System.out.println("this is n() method");  
                }
            {  
                public void run(){  
                        m();//calling method of Reentrant class  
                }  
            };  
        }  
        class ReentrantExample
        {  
            public static void main(String args[])
            {  
                Reentrant re=new Reentrant();  

                Thread t1=new Thread();

                t1.start();  
            }
        } 
9
  • 1
    you have a } before the run method that needs to be removed it seems. Commented Sep 5, 2018 at 7:04
  • 1
    remove the { and }; that you've put around that method Commented Sep 5, 2018 at 7:05
  • This braces is for starting of Anonymous class Commented Sep 5, 2018 at 7:06
  • 1
    @Yogesh that might be your intent, but it's not the syntax of an anonymous class, but of a block. Check this link for more information on anonymous classes docs.oracle.com/javase/tutorial/java/javaOO/… Commented Sep 5, 2018 at 7:07
  • 1
    you can't declare methods inside methods. sure, you can create an anonymous class which contains them, just not directly Commented Sep 5, 2018 at 7:17

1 Answer 1

1

Change your code,

{  
            public void run(){  
                    m();//calling method of Reentrant class  
            }  
        };

to

  public void run(){  
                    m();//calling method of Reentrant class  
            }  
Sign up to request clarification or add additional context in comments.

1 Comment

how is this using an anonymous class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.