what is the rule for the access specifier for the constructor? eg if class has public specifier then does this imply that all the construtors of the class are also public.And if yes can we change it?
SCJP 5<br />SCWCD 1.4
vignesh hariharan
Ranch Hand
Joined: Jun 23, 2005
Posts: 77
posted
0
no.. the class scope is different and the constructor scope is different.. even if u have a public class.. u can name ur constructor private..
Regards,
vignesh
Sandeep Vaid
Ranch Hand
Joined: Feb 27, 2006
Posts: 390
posted
0
Class Access Specifier has nothing to do with Constructor Access Specifier.
Example :
In the above example access specifier for class Temp is public but it's constructor is private. Even by default constructor will get the default specifier (which doesn't depends on class's specifier)
Marino Christe
Greenhorn
Joined: Apr 15, 2006
Posts: 17
posted
0
hey if u have a class with default access and if u dont provide any constructor, then the automatically created constructor has the same accees as the class, i.e., default. but if u write ur own constructor, u can write it wid any other access specifier also.
1)If you explicitly define any constructor for the class then the accessability modifier of the defined constructor(or constructors) is completely independent of the accessability modifier of the class.
2)If you want the compiler to generate a constructor for you(ie you are not defining even a single constructor)then the accessability modifier of the generated constructor is dependent on the accessability modifier of the class.
3)Access mode of the generated constructor is public for public classes, and default for classes with any other access mode.
Hope It is clear. Let me know If I am wrong.
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
Access mode of the generated constructor is public for public classes, and default for classes with any other access mode.
Actually according to the Java Language Specification, the access modifier of the default constructor is the same as the access modifier of the class.
Aniket Patil
Ranch Hand
Joined: May 02, 2006
Posts: 218
posted
0
Hi Sandeep,
Class Temp1 will not be able to create an object of class Temp, as the constructor of Temp is private. In fact, class Temp1 should not be able to even "see" a constructor of Temp, since it is marked private.
Only the members of Temp would now able to create an instance of Temp.
SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Girish Nagaraj
Ranch Hand
Joined: Apr 19, 2006
Posts: 153
posted
0
Hi Keith,
I think we have to Check out for inner classes.
Bcoz inner classes can have all(public, protected, default/package, private)access modifiers.
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
Originally posted by Girish Nagaraj: Hi Keith,
I think we have to Check out for inner classes.
Bcoz inner classes can have all(public, protected, default/package, private)access modifiers.
This is the link to what the Java Language Specification says.
I checked out for inner classes with diff. modifiers. What you said was right. I have modified my points accordingly, Let me know if their is any mistake.
1)If you explicitly define any constructor for the class then the accessability modifier of the defined constructor(or constructors) is completely independent of the accessability modifier of the class.
2)If you want the compiler to generate a constructor for you(ie you are not defining even a single constructor)then the accessability modifier of the generated constructor is dependent on the accessability modifier of the class.
3)The access modifier of the generated constructor is the same as the access modifier of the class.