Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor accessor

 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Pals, here is a question from JTips.net QUIZ4:
<code>
public class Test {
double x, y;
Test() {
super();
}
}
The No arguments constructor Test() has "default" access modifier ( i.e with no access modifier ). True or False?
</code>
The answer is false, but i think it should be true. Anybody give a light?
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Li,
What is meant Pals? Clarify.
Test() should have the default access modifier. I don't see any othe reason to be the answer false. Someone else ....

------------------
azaman
 
Percy Densmore
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
The way I understand it is, if a constructor has no access modifier, it takes on the access modifier of it's parent class, so to speak. Check out this previous discussion on the same topic www.javaranch.com/ubb/Forum24/HTML/003754.html
Hope this helps :-)
Percy
[This message has been edited by Percy Densmore (edited July 17, 2001).]
[This message has been edited by Percy Densmore (edited July 17, 2001).]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is incorrect.
It is true that the default constructor has the same access modifier as the class... but that only applies when you do not code a no parameter constructor!!!
In the example, the constructor will have a modifier of default (package) and will not be accesible from outside the package that the class is in. Example:

Will give this error message:
TestTest.java:6: Test() is not public in test.Test; cannot be accessed from outside package
Test t = new Test();
^
1 error

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic