aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "about constructor" Watch "about constructor" New topic
Author

about constructor

Nilesh Shekokar
Greenhorn

Joined: May 02, 2006
Posts: 17
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
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
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
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.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Check All Possible Combinations of Features and Modifiers.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
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
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
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
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
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.

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#16823
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Hi keith,

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.

Hope It is clear.
Let me know If I am wrong.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: about constructor
 
Similar Threads
A small doubt
public private constructor
Constructors
Constructor
Two classes with public access specifier in a single source file