Java Programming - Declarations and Access Control - Discussion


Which of the following is/are legal method declarations?

  1. protected abstract void m1();
  2. static final void m1(){}
  3. synchronized public final void m1() {}
  4. private native void m1();

[A]. 1 and 3
[B]. 2 and 4
[C]. 1 only
[D]. All of them are legal declarations.

Answer: Option D


All the given statements are legal declarations.

Sainath said: (Jul 8, 2011)  
Can you explain it in detail?

Vinu said: (Jul 12, 2011)  
Can any one explain ?

Sush said: (Aug 4, 2011)  
Please anyone explain it?

Rocks said: (Jan 15, 2012)  
What is the working of synchronize and native?

Adrian said: (May 1, 2012)  
Synchronize and native works because synch blocks the execution of other methods on the same class (for the duration of the method). Maybe the native method wants to modify a shared resource.

Venkat said: (Feb 9, 2013)  
1. protected abstract void m1();
Is correct because abstract method may be implemented in subclass and protected allows it to do so.
2. static final void m1(){}
Is also correct because final does not allow method to be changed and static does not user to another method with same name.
3. synchronized public final void m1() {}
It allow only one instance of method to be executed at a time.
4. private native void m1();
It allows method written in another programming language to be executed in java

Chandra Bhushan said: (Feb 18, 2013)  
But as I can see the question is asked for declaration only and not the definition of function.

Limia said: (Jun 16, 2013)  
Shouldn't access specifiers like public, private and protected be the first words in a declaration statement? Please correct me if I'm wrong, I read this somewhere.

Manohar said: (Aug 9, 2013)  
static final void m1(){}

Can anyone explain it clearly?

Kaustubh Kaley said: (Feb 21, 2014)  
Why are {} specified in the declaration?

Amaziane said: (Jun 28, 2014)  
I have some doubt that the declaration protected abstract void m1();

Compile, at least in my workspace with jdk7.

Karthik said: (Sep 30, 2015)  
Can you explain it clearly?

Jatinder Verma said: (Feb 19, 2016)  
There are only two method declarations:

1. Protected abstract void m1();

2. Private native void m1();

Except these all others are methods with definition.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.